如何使用解析数据库在iOS应用中实现好友添加/删除功能? [英] How to implement friend add/remove functionality in an iOS app using parse database?
问题描述
我正在尝试实现存储有关用户朋友(请求朋友,已接收朋友请求,已接受请求)的数据的功能.我有两个桌子.
第一个表是_User with username列.
第二个表是朋友".该表跟踪谁是用户的朋友.该表有两个字段:
- 指向用户表
的指针列
- 称为receivedRequest的数组列.在此数组中,我保留了_User的objectId,谁将请求发送给该用户.
表_User与Friends表具有一对一关系,但同时存在一个数组字段,用于保存用户朋友的信息.在此数组中,我正在保存其他用户的objectId.我使用数组来避免为每个朋友的请求重复行.
拳头我想知道这是个好主意还是有其他更好的选择.实际上,我有多余的数组列,它们是已接收的请求.发送请求.并接受了请求.它们都是数组.
第二,我想知道如何编写查询以转到朋友"表.查找当前用户行.转到friendList列.返回名称在该数组中的每个朋友的名字?
目前我正在这样做:
- (PFQuery *)queryForTable {
//Query user's friend request
PFQuery *query = [PFQuery queryWithClassName:@"Friends"];
[query whereKey:@"user" equalTo:[PFUser currentUser]];
[query includeKey:@"receivedRequest"];
return query;
}
这仅返回当前用户添加的使用ID.我需要_User表中的名字.
这就是我要做的:
(1)User类应该与用户与应用程序的关系有关,这是一个位于用户和应用程序之间的数据位置.
(2)对于用户要共享的数据,请拥有一个Persona类,该类具有图像,昵称等.Persona应该包含一个指向User的指针,反之亦然.
(3)女神异闻录(常用的女神异闻录)发出朋友邀请并成为朋友.
(4)字符串对象ID的数组=不好,指针的数组=好的.实际上,我无法想到在这种情况下,我更喜欢使用字符串对象id而不是指针.
(5)FriendInvitation应该是它自己的对象,其中邀请者和被邀请者是指向Persona的指针.
(6)友谊是双边和对称的关系(至少我们一直希望这样).一个很好的表示形式可能是Friendship类,该类具有一个数组的指针,该指针恰好指向两个Persona对象.
以下是给定数据模型的一些功能:
Persona有一个指向User的指针,将其称为"user
",而User有一个persona
指针. FriendInvitation有一个inviter
和invitee
,它们都是指向Persona的指针.友谊有两个指向Persona的指针的数组,称为friends
// get the current user's FriendInvitations
- (void)friendInvitationsWithCompletion:(void (^)(NSArray *, NSError *))completion {
PFObject *persona = [PFUser currentUser][@"persona"];
PFQuery *query = [PFQuery queryWithClassName:@"FriendInvitation"];
[query whereKey:@"invitee" equalTo:persona];
[query includeKey:@"inviter"];
[query findObjectsInBackgroundWithBlock:completion];
}
// get the current user's friendships
// remember, these are not the friends, but the objects that record pairings of friends.
// see the next function for the friends
- (void)friendshipsWithCompletion:(void (^)(NSArray *, NSError *))completion {
PFObject *persona = [PFUser currentUser][@"persona"];
PFQuery *query = [PFQuery queryWithClassName:@"Friendship"];
[query whereKey:@"friends" equalTo:persona];
[query includeKey:@"friends"];
[query findObjectsInBackgroundWithBlock:completion];
}
// get the current user's friends' personae
- (void)friendsWithCompletion:(void (^)(NSArray *, NSError *))completion {
PFObject *persona = [PFUser currentUser][@"persona"];
[self friendshipsWithCompletion:^(NSArray *friendships, NSError *error) {
if (!error) {
NSMutableArray *result = [@[] mutableCopy];
for (PFObject *friendship in friendships) {
NSArray *friends = friendship[@"friends"];
NSInteger indexOfFriend = ([friends indexOfObject:persona] == 0)? 1 : 0;
[result addObject:friends[indexOfFriend]];
}
completion(result, nil);
} else {
completion(nil, error);
}
}];
}
// agree to be friends with someone
- (void)becomeFriendsWith:(PFObject *)friend completion:(void (^)(BOOL, NSError *))completion {
PFObject *persona = [PFUser currentUser][@"persona"];
PFObject *friendship = [PFObject objectWithClassName:@"Friendship"];
friendship[@"friends"] = @[ persona, friend ];
[friendship saveInBackgroundWithBlock:completion];
}
// we could go on, but this should convey the basic ideas
I am trying to implement functionality to store data about user's friends (request friend, received friend request, accepted request). I have two tables.
First table is _User with username column.
Second table is Friends. This table is keeping track of who are friends of the user. This table has two fields:
- A pointer column to user table
- An array column called receivedRequest. In this array I keep the _User's objectId who are send request to that user.
Table _User has one to one relation with Friends table but meanwhile there is an array field keeping information of user's friend. In this array I am saving objectId of other users. I am using an array to avoid repeating rows for each friend's request.
Fist I want to know if this is a good idea or there is any alternative better that this. Actually I have extra array columns which is recived requests. Send requests. And accepted requests. All of them are array.
Second I want to know how can I write a query to go to Friends table. Find current user row. Go to friendList column. Return name of each friends whose name is in that array?
Currently I am doing this:
- (PFQuery *)queryForTable {
//Query user's friend request
PFQuery *query = [PFQuery queryWithClassName:@"Friends"];
[query whereKey:@"user" equalTo:[PFUser currentUser]];
[query includeKey:@"receivedRequest"];
return query;
}
This is returning only Id of the use's added my current user. I need their name from _User table.
Here's what I'd do:
(1) the User class ought to be about the user's relationship with the app, a place for data that's just between the user and the app.
(2) For data that users want to share, have a Persona class that has an image, nickname, etc. Persona should contain a pointer to User and vice versa.
(3) Personae (Personas in common usage) make friend invitations and become friends.
(4) arrays of string object ids = bad, arrays of pointers = good. In fact, I can't think of a circumstance where I'd prefer a string object id over a pointer.
(5) A FriendInvitation ought to be its own object, where inviter and invitee are pointers to Persona.
(6) A friendship is a bilateral and symmetrical relationship (at least we always hope they are). A good representation for that might be a Friendship class that has an array of pointers to exactly two Persona objects.
Here are a few functions, given a data model:
Persona has a pointer to User, call it 'user
', and User has a persona
pointer. FriendInvitation has an inviter
and invitee
, both pointers to Persona. Friendship has an array of two pointers to Persona, call it friends
// get the current user's FriendInvitations
- (void)friendInvitationsWithCompletion:(void (^)(NSArray *, NSError *))completion {
PFObject *persona = [PFUser currentUser][@"persona"];
PFQuery *query = [PFQuery queryWithClassName:@"FriendInvitation"];
[query whereKey:@"invitee" equalTo:persona];
[query includeKey:@"inviter"];
[query findObjectsInBackgroundWithBlock:completion];
}
// get the current user's friendships
// remember, these are not the friends, but the objects that record pairings of friends.
// see the next function for the friends
- (void)friendshipsWithCompletion:(void (^)(NSArray *, NSError *))completion {
PFObject *persona = [PFUser currentUser][@"persona"];
PFQuery *query = [PFQuery queryWithClassName:@"Friendship"];
[query whereKey:@"friends" equalTo:persona];
[query includeKey:@"friends"];
[query findObjectsInBackgroundWithBlock:completion];
}
// get the current user's friends' personae
- (void)friendsWithCompletion:(void (^)(NSArray *, NSError *))completion {
PFObject *persona = [PFUser currentUser][@"persona"];
[self friendshipsWithCompletion:^(NSArray *friendships, NSError *error) {
if (!error) {
NSMutableArray *result = [@[] mutableCopy];
for (PFObject *friendship in friendships) {
NSArray *friends = friendship[@"friends"];
NSInteger indexOfFriend = ([friends indexOfObject:persona] == 0)? 1 : 0;
[result addObject:friends[indexOfFriend]];
}
completion(result, nil);
} else {
completion(nil, error);
}
}];
}
// agree to be friends with someone
- (void)becomeFriendsWith:(PFObject *)friend completion:(void (^)(BOOL, NSError *))completion {
PFObject *persona = [PFUser currentUser][@"persona"];
PFObject *friendship = [PFObject objectWithClassName:@"Friendship"];
friendship[@"friends"] = @[ persona, friend ];
[friendship saveInBackgroundWithBlock:completion];
}
// we could go on, but this should convey the basic ideas
这篇关于如何使用解析数据库在iOS应用中实现好友添加/删除功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!