解析-保存相关对象 [英] Parse - saving related objects

查看:71
本文介绍了解析-保存相关对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目的后端使用了Parse.

I'm using Parse for the back end in my project.

您会想象数据模型中有很多关系.很多时候,我会同时创建一个父"对象及其所有子对象",并将它们全部保存到Parse中.

As you would imagine there are quite a few relations in the data model. A lot of the time I create a "parent" object and all of its "children" at the same moment and save them all to Parse.

现在,在执行此操作时是否有必要单独保存孩子?文件等也一样...

Now, when doing this is it necessary to save the children individually? The same for files etc...

第一个示例-向用户对象添加头像

UIImage *image = // image from camera
NSData *pngData = UIImagePNGRepresentation(image);
PFFile *imageFile = [PFFile fileWithData:pngData];
[[PFUser currentUser] setObject:imageFile forKey:"avatar"];

好,因此在设备上,我可以引用用户上的@"avatar"键并获取头像文件.但是,应该如何将其保存到Parse?

OK, so on the device I can reference the @"avatar" key on the user and get the avatar file. But how should this be saved to Parse?

如果我愿意...

[[PFUser currentUser] saveInBackground];

这将保存添加的新文件吗?还是我需要先保存文件并等待此操作成功,然后再将其添加到用户对象中,然后再保存用户对象?

Will this save the new file that was added? Or do I need to save the file first and wait for this to succeed before then adding it into the user object and then saving the user object?

第二个示例

创建对象树...

PFObject *family = [PFObject objectWithClassName:@"Family"];
[family setObject:@"Smith" forKey:@"familyName"];

PFObject *person1 = [PFObject objectWithClassName:@"Person"];
[person1 setObject:@"Bob" forKey:@"name"];

PFObject *person2 = [PFObject objectWithClassName:@"Person"];
[person2 setObject:@"Alice" forKey:@"name"];

PFObject *person3 = [PFObject objectWithClassName:@"Person"];
[person3 setObject:@"Chris" forKey:@"name"];

[family setObject:@[person1, person2, person3] forKey:@"members"];

我将如何保存该对象集合?

How would I save this collection of objects?

我可以只做[family saveInBackground];吗?

还是我必须首先保存每个Person对象并在保存family对象之前检查其是否工作?

Or do I have to go through a process of saving every Person object first and checking that it worked before saving the family object?

推荐答案

只要父子关系是Pointer,就不必先保存子级. PFRelation的工作方式有所不同,但是在父对象上进行保存还会将相关的子级保存为指针.对于Cloud Code来说,这是正确的,而且我很确定它在设备上也适用.

As long as the relationship between parent and child is Pointer, you don't have to save the child first. PFRelation works differently, but a save on the parent object will also save children related as pointers. This is true for Cloud Code, and I am pretty sure it holds true for the device as well.

此答案中的一些详细信息: https://www.parse. com/questions/cloud-code-efficiency-hierarchy-

Some details in this answer: https://www.parse.com/questions/cloud-code-efficient-hierarchy-saving

这篇关于解析-保存相关对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆