SwiftyParse 1.0.6

SwiftyParse 1.0.6

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后发布2016年2月
SPM支持 SPM

RexSheng 维护。



  • 作者
  • Rex Sheng

SwiftyParse

安装

使用方法

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 }
        }
    }
}

操作同时也在影响本地存储中的数据。

低级查询/操作

出于某种原因,您可能想坚持使用纯查询和 JSON,以下是可以采取的措施

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, ...)

注意

我已在 Parse 应用设置中禁用“需要可撤销会话”,以便登录/注册工作正常。欢迎贡献力量。

作者

Rex Sheng

许可证

SwiftyParse 在 MIT 许可证下可用。有关更多详细信息,请参阅 LICENSE 文件。