0
我有一個parse.com後端。我有一個Message類,它是PFObject的子類。我將數據庫中的消息存儲在數組messages = Message中。如何在使用子類時更新解析記錄
當我需要更新記錄,因爲我得到重複,我有麻煩。
我一直在試圖做的是從消息數組中獲取消息,然後調用saveInBackgroundWithBlock ...思考後端會知道它指向數據庫中的現有行。
我也剛剛遇到fetchIfNeeded。
我的代碼如下。我哪裏錯了?我應該如何更新本地存儲在消息數組中的現有記錄。我應該如何在我的場景中使用fetchIfNeeded?還是應該以某種方式使用核心數據/本地存儲? (迷茫)
class Message : PFObject, PFSubclassing {
var messageTitle : NSString
var imageFile: PFFile
}
func getMessages() {}
var currentMessages = [Message]()
var messageQuery = Message.query()
messageQuery.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
for object in objects {
var message = Message()
message.messageTitle = object["messageTitle"] as String
message.imageFile = object["imageFile"] as PFFile
self.currentMessages.append(message)
}
}
func updateMessage() {
message.messageTitle = self.messageTitle.text
let imageData = UIImagePNGRepresentation(self.imageToPost.image)
let imageFile = PFFile(name: "image.png", data: imageData)
message.imageFile = imageFile
message.saveInBackgroundWithBlock({ (success: Bool!, error: NSError!) -> Void in
if success == false {
self.displayAlert("Something went wrong", error: "Please try again later")
} else {
self.navigationController?.popViewControllerAnimated(true)
}
})
}
func saveNewMessage() {
message = Message()
message.messageTitle = messageTitle.text
let imageData = UIImagePNGRepresentation(self.imageToPost.image)
let imageFile = PFFile(name: "image.png", data: imageData)
message.imageFile = imageFile
message.saveInBackgroundWithBlock({ (success: Bool!, error: NSError!) -> Void in
if success == false {
self.displayAlert("Something went wrong", error: "Please try again later")
} else {
self.navigationController?.popViewControllerAnimated(true)
}
})
}
關於'fetchIfNeeded',如果需要(即,如果它尚未被提取),它將把指針變成完整對象。您的代碼不需要。 –