2

我正在使用 swift 和 Parse Framework (1.6.2)。我将数据存储到本地数据存储中。数据由几个字符串和一个图像组成。此图像将存储在 PfFile 中,然后将添加到 PfObject。

现在我想知道为什么图像没有被保存,直到我注意到我需要调用 PFFile 的 save() 方法。问题是图像似乎被上传到我不想要的 Parse。我希望它存储在本地设备上..

有解决方案吗?

谢谢

编辑:

代码是这样工作的:

var spot = PFObject(className: "Spot")
let imageData = UIImageJPEGRepresentation(spotModel.getPhoto(), 0.05)
let imageFile = PFFile(name:"image.jpg", data:imageData)

spotObj.setObject(spotModel.getCategory(), forKey: "category")
spotObj.setObject(imageFile, forKey: "photo")


//if I simply would do this, the PFObject will be saved into the local datastorage, but the image won't be saved
spotObj.pin()

//if I do this, the image will be saved also BUT it won't be saved into the local data storage but will be uploaded to parse
imageFile.save()
spotObj.pin()
4

1 回答 1

1

好的,看看这里使用 PFFile (Parse Local Datastore) 最终保存在 PFObject 上?. 可以肯定的是,如果您在 PFFile 上调用 save ,它将保存到在线数据存储区。所以你应该使用PFFile.save()。我认为对您来说最好的选择是将文件保存在某个文件夹中。在本地并将该路径保存在 PFObject 中。解析文档就这么说

"Pinning a PFObject is recursive, just like saving, so any objects that are pointed to by the one you are pinning will also be pinned. When an object is pinned, every time you update it by fetching or saving new data, the copy in the local datastore will be updated automatically. You don't need to worry about it at all."

它递归地调用主 PFObject 中其他对象的 .pin()。但是,如果您查看PFFile API 文档,它没有 .pin() ,这意味着它不支持将 PFFile 保存到本地数据存储区。所以我会说你应该将它们保存在某个目录中并将路径保存在你的 PFObject 中。

更新

save:

Saves the file synchronously and sets an error if it occurs.

- (BOOL)save:(NSError **)error
Parameters
error
Pointer to an NSError that will be set if necessary.

Return Value
Returns whether the save succeeded.
于 2015-01-25T12:44:49.533 回答