2015-09-07 79 views
0

我正在尝试对Parse中已存储在核心数据中的数据进行更改。但它没有做出必要的修改。我在这里查看关于对象的解析文档:https://parse.com/docs/ios/guide#objects。而且我似乎正在做文件告诉我要做的事情。也许我错过了什么?这是我的代码:解析不更新核心数据Swift

import UIKit 
import Parse 

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     var products = PFObject(className:"Products") 
     products["name"] = "ice cream" 
     products["description"] = "Strawberry" 
     products["price"] = 4.99 
     products.saveInBackgroundWithBlock { 
      (success, error) -> Void in 
      if (success) { 
       println("Object saved with id \(products.objectId)") 
      } else { 
       println("Not successful!") 
       print(error) 
      } 
     } 

//ignore the code below this line for now please :) 

     var query = PFQuery(className: "Products") 

     query.getObjectInBackgroundWithId("7lmnxHxibK", block: { (object: PFObject?, error) -> Void in 
      if error != nil { 
       print(error) 
      } else if let product = object { 
       print("YO") 
       product["description"] = "Rocky Road" 
       product["price"] = 5.99 
       products.saveInBackground() 
      } 
     }) 
    } 
} 

因此,上面的代码创建了一个ID为7lmnxHxibK的对象。 descriptionStrawberrynameice cream,并且price4.99。它应该如此。所以现在作为一个尝试与ID 7lmnxHxibK改变对象的属性,我写了下面的代码:

var query = PFQuery(className: "Products") 

query.getObjectInBackgroundWithId("7lmnxHxibK", block: { (object: PFObject?, error) -> Void in 
    if error != nil { 
     print(error) 
    } else if let product = object { 
     print("YO") 
     product["description"] = "Rocky Road" 
     product["price"] = 5.99 
     products.saveInBackground() 
    } 
}) 

此代码应该对象进行必要的修改id为7lmnxHxibK。但不是对对象的属性进行必要的更改,而是创建一个新对象,其中包括description,nameprice,全部为(undefined)。任何人都有解决方案来解决这个问题?

+0

呵呵,你在关注Rob Percival的课程吗? –

+0

只是为了确保,你正试图编辑这个'对象',在这种情况下,草莓冰淇淋是正确的? –

+0

哈哈,是的,我是。你还好吗?如果是这样,你怎么看待它? – Jae

回答

0

您必须将products.saveInBackground()更改为product.saveInBackground()。同样在你调用第二个查询解析时,可能还没有完成第一次保存对象。