Parse - 使用关系还是指针? [英] Parse - Using relations versus pointers?

查看:18
本文介绍了Parse - 使用关系还是指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Parse 作为我的后端.我在设置对象之间的正确关系时遇到问题.

I am using Parse as my backend. I have problems setting up the correct relation between objects.

我基本上有一个名为 Post 的类,每个帖子都属于一个用户(PFUser),并且在获取帖子时我希望用户信息与帖子一起获取.

I basically have a class named Post, each post belongs to a user(PFUser), and when fetching a post I want the user info to be fetched along with the post.

@interface Post : PFObject<PFSubclassing>

@property (nonatomic, strong) NSDate *time;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *body;
@property (nonatomic, strong, readonly) PFRelation *user;
// User in backed is defined as a relationship to _user    

@end

// Saving the post
[post.user addObject:[PFUser currentUser];
[post saveInBackground];

这工作正常并将帖子与该用户相关联,但是当我稍后尝试获取帖子时,似乎我无法从 PFRelation 获取 _user 的实例.

This works fine and relates the post to that user, but when I try to fetch the post later, it doesn't seem like I can get an instance of _user from PFRelation.

处理这个问题的正确方法是什么?试图将 PFRelation 更改为 PFUser 但这会崩溃,因为它试图调用 PFUser 对象上的保存

What is the correct way to handle this? Tried to change PFRelation to PFUser but that would crash because it tries to call save on the PFUser object

推荐答案

Relation 用于当您需要一长串相关类的列表,其中数组不起作用时,或者当您想根据需要查询相关对象时并且每次加载包含对象时都不要包含列表.

A Relation is for when you want a long list of related classes, where an array doesn't work, or when you want to query the related objects as needed and not have the list included every time you load the containing object.

Parse 基本上有 4 个选项:

Basically you have 4 options with Parse:

  • 指针 - 对另一个类的单一引用(1 到 0..1)
  • 数组 - 指针集合,每次加载对象(1 到 0..n,小列表)
  • 关系 - 指针的集合,就像 SQL 中的连接表(在幕后为您处理),您必须针对它运行查询以加载值(1 到 0..n)
  • 自定义连接类 - 实际上只是另一个对象(如 SQL 中的多对多连接),每个对象都有一个指针以及任何相关信息(1..n 到 1..n)

在你的情况下,一个简单的指针会做你想做的事.

In your case a simple Pointer would do what you want.

这篇关于Parse - 使用关系还是指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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