2015-12-29 54 views
1

我從Parse獲取對象並將其顯示在我的UI中。現在我正在將數據保存在Parse的Local DataStorage中。我看了下面的解析示例:如何從Parse獲取多個對象以保存Swift中的本地DataStorage?

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! 

       } 
      } 
     } 
    } 

在我上面的代碼中,我怎麼能保存所有提取的對象objModalClass在本地數據存儲。請詳細解釋。

回答

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.   
} 
相關問題