GithubHelp home page GithubHelp logo

b051 / parse.swift Goto Github PK

View Code? Open in Web Editor NEW

This project forked from greycats/parse.swift

0.0 2.0 1.0 117 KB

A Parse REST client written in Swift. Protocol orientated / Local query and persist / pyramid-like

License: MIT License

Objective-C 0.74% Swift 98.30% Ruby 0.96%

parse.swift's Introduction

SwiftyParse

Build Status Version License Platform

Installation

SwiftyParse is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SwiftyParse"

Usage

import SwiftyParse

// first of all, set up Parse using RestKey
Parse.setup(applicationId: "<# your application id #>", restKey: "<# rest key #>")
// or MasterKey
Parse.setup(applicationId: "<# your application id #>", masterKey: "<# master key #>")

// Models natually have objectId, createdAt, updatedAt and security fields. And File, User, Installation, Push models are already defined for you.
// You can create your model as class or struct

struct Document: ParseObject {
	static let className = "Document"
	var json: Data!
	init() {}
	let title = Field<String>("title")
	let description = Field<String>("description")
	let author = Field<User>("author")
}

// you can optionally pre-load all documents to file, and queries later on will be performed locally as much as possible, updates and creations will also affect this local storage.
Document.persistent(86400)

// To log in user
User.logIn("username", password: "password") { user, error in
	// Let us assume this is a valid user
	guard let user = user else { return }
	// to perform a query
	Document.query().list { documents, error in
	    ...
	}
	Document.query().local(false).order("-updatedAt,title").relatedTo(user, key: "master_piece").list { documents, error in
	    for document in documents {
	        print(document.title.get())
	    }
	}
	
	// For more query options, see Query.swift
	
	// to create a document
	Document.opertation().set("author", value: user).save { document, error in 
		if let document = document {
			// update this document with new title
			document.title.set("SwiftyParse")
			document.save { error in
	            ...
	       }
	   }
	}
	
	// and much more features you can discover
	let firstNameQuery = User.query().whereKey("first_name", equalTo: firstName).whereKey("birth", greaterThan: birth)
	let lastNameQuery = User.query().whereKey("last_name", equalTo: lastName).whereKey("birth", greaterThan: birth)
	let authorQuery = firstNameQuery || lastNameQuery
	Document.query().whereKey("author", matchKey: "id", inQuery: authorQuery).list { documents, error in
	    for document in documents {
	        document.operation().setSecurity(me).save { _ in }
	    }
	}
}

Operations are affecting data in local storage too.

Low-Level Queries/Operations

For some reason, you might want to stick with plain queries and JSON, here is what you can do:

let group = dispatch_group_create()
let author = Pointer(className: "_User", objectId: "1234abcd")
_Query(className: "Document", constraints: .EqualTo("author", author)).each(group) { (json: [String: AnyObject]) in
	guard let objectId = json["objectId"] as? String else { return }
	_Operations(operations: [.ClearSecurity]).update("Document", objectId: objectId) { _ in
	}
}
// By using `each`, you can iterate over every record of every pages
dispatch_group_notify(group, ...)

Notice

I disabled "Require revocable sessions" in the Parse app settings to get logIn/signUp worked properly. Contribution is welcome.

Author

Rex Sheng

License

SwiftyParse is available under the MIT license. See the LICENSE file for more info.

parse.swift's People

Contributors

b051 avatar

Watchers

 avatar James Cloos avatar

Forkers

d-e-f-e-a-t

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.