如何从Parse中获取多个对象以保存在Swift中的Local DataStorage中?

时间:2015-12-29 18:41:45

标签: ios swift parse-platform

我从Parse获取对象并在我的UI中显示它。现在我正在努力将数据保存在Parse的Local DataStorage中。我看了下面的Parse示例:

let query = PFQuery(className: "GameScore")
query.fromLocalDatastore()
query.getObjectInBackgroundWithId("xWMyZ4YE").continueWithBlock {
(task: BFTask!) -> AnyObject in
if let error = task.error {
    // Something went wrong.
    return task;
}

// task.result will be your game score
return task;
}

以上示例是否用于获取1个对象。我不知道如何为多个对象做同样的事情。我通过以下代码获取对象:

let query:PFQuery = PFQuery(className: "Events")
    query.findObjectsInBackgroundWithBlock {
    (object, error) -> Void in
        if object != nil
        {
            if(object!.count != 0)
            {
                for messageObject in object! {
                    let eventName:String? = (messageObject as! PFObject)["EventName"] as? String
                    let createdBy:String? = (messageObject as! PFObject)["CreatedBy"] as? String
                    let eventDate:String? = (messageObject as! PFObject)["EventDate"] as? String
                    objModalClass.eveName = eventName!
                    objModalClass.crtedBy = createdBy!
                    objModalClass.eveVenue = eventVenue!

                }
            }
        }
     }

在上面的代码中,如何在Local DataStorage中objModalClass中保存所有获取的对象。请详细解释。

1 个答案:

答案 0 :(得分:0)

你应该看看对象固定。只要您启用了本地数据存储,就可以通过引脚名称检索对象,以及通过取消固定从本地数据存储中释放这些对象。来自文档:

“以递归方式将对象及其指向的每个对象异步存储在本地数据存储区中。”

对于单个对象:

public func pinInBackgroundWithName(name: String, block:PFBooleanResultBlock?)

对于许多物体:

public class func pinAllInBackground(objects: [AnyObject]?, withName name: String, block: PFBooleanResultBlock?)

补充unpinWithName(name: String)可用于两种情况。固定仅适用于PFObject及其子类。

你也可以从像这样的引脚名称查询

var query = PFQuery.queryWithClassname("PFObject_subclass")
query.fromPinWithName("Your_given_pin_name")
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
    //objects from pin name returned.        
}