【发布时间】:2016-06-30 09:18:30
【问题描述】:
我将数据作为数组存储在 NSUserDefaults 中。我需要使用 Parse Server 获取这些数据并将其存储在数据库中。当我尝试遍历数组并使用 saveInBackgroundWithBlock 时,循环再次运行,在块完成之前设置新值。将此数据作为单个对象保存在数据库中的最佳方法是什么?
let other = PFObject(className: "Other")
if (NSUserDefaults.standardUserDefaults().objectForKey("otherTypes") != nil) && (NSUserDefaults.standardUserDefaults().objectForKey("otherCosts") != nil) {
otherCosts = NSUserDefaults.standardUserDefaults().objectForKey("otherCosts") as! [Double]
otherTypes = NSUserDefaults.standardUserDefaults().objectForKey("otherTypes") as! [String]
for costs in otherCosts {
other.setObject(PFUser.currentUser()!.objectId!, forKey: "userId")
other.setObject(otherTypes[i], forKey: "otherName")
let cost = String(costs)
other.setObject(cost, forKey: "otherCost")
i = i + 1
other.saveInBackgroundWithBlock({ (success, error) -> Void in
if error == nil {
print("Success")
NSUserDefaults.standardUserDefaults().setObject(nil, forKey: "otherTypes")
NSUserDefaults.standardUserDefaults().setObject(nil, forKey: "otherCosts")
} else {
print("Fail")
}
})
}
【问题讨论】:
-
查看 NSCondition 类,它允许一个线程等待其他线程的完成。使用它,您可以等待之前的 save() 完成,然后再开始循环。
-
您想要 Parse 中的多个
Other对象还是一个包含两个数组的对象?
标签: ios arrays swift parse-platform