简体   繁体   English

如何按用户对PFObject进行分组,然后对其进行排序?

[英]How can I group PFObjects by User and then Sort them?

I am currently building a simple messaging service for iOS with Parse where users send alerts to each other. 我目前正在使用Parse构建适用于iOS的简单消息服务,用户可以相互发送警报。

On the main screen the alerts will be grouped by user and previously shown alerts will be shown as well. 在主屏幕上,警报将按用户分组,并且还将显示以前显示的警报。

Recent Alerts 最近的警报

Bob - 2
Jim - 3
Tom
Bill

Currently I am fetching the items by date and grouping them by userId like this: 目前,我正在按日期获取项目,并按如下所示的userId进行分组:

{
"USER_ID1":[
{
"SENDER_NAME":"JOHN",
"SHOWN":0
},
{
"SENDER_NAME":"JOHN",
"SHOWN":1
}
],
"USER_ID2":[
{
"SENDER_NAME":"BOB",
"SHOWN":0
},
{
"SENDER_NAME":"BOB",
"SHOWN":0
}
]...
}

This is how the bit fetching the alerts works 这就是获取警报的位的工作方式

PFQuery *query = [PFQuery queryWithClassName:@"Alerts"];
[query whereKey:@"recipiantIds" equalTo:[[PFUser currentUser] objectId]];
[query orderByAscending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    self.alerts = [NSMutableArray arrayWithArray:objects];
    if (error) {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
        [[[UIAlertView alloc] initWithTitle:@"Could not fetch alerts" message:@"Please check your internet connection" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
    }
    else {
        for (PFObject *alert in objects) {
            [self addAlert:alert[@"sender_name"] andShown:alert[@"shown"] andId:alert.objectId andSenderId:alert[@"senderId"] andDate:alert.createdAt];
        }

        [self.tableView reloadData];
    }
}];

And the function for adding alerts to the Dictionary: 以及向字典添加警报的功能:

- (void)addAlert:(NSString *)senderName andShown:(NSNumber*)shown andId:(NSString*)alertId andSenderId:(NSString*)senderId andDate:(NSDate *)date {
    NSDictionary *items;
    if (shown != nil) {
        items = @{@"sender":senderName, @"shown":shown, @"alertid":alertId, @"senderId":senderId, @"datestamp":date};
    }
    else {
        items = @{@"sender":senderName, @"alertid":alertId, @"senderId":senderId, @"datestamp":date};
    }
    if ([self.myalerts objectForKey:senderId] != nil) {
        NSMutableArray *userAlerts = [self.myalerts objectForKey:senderId];
        [userAlerts addObject:items];
        [self.myalerts setObject:userAlerts forKey:senderId];
    }
    else {
        NSMutableArray *userAlerts = [NSMutableArray new];
        [userAlerts addObject:items];
        [self.myalerts setObject:userAlerts forKey:senderId];
    }
}

If there is any nice way to sort them like SnapChat does I'd love to know it. 如果有什么好办法可以像SnapChat一样对它们进行排序,我很想知道这一点。

Thanks guys! 多谢你们!

Yes, this can be done. 是的,可以这样做。 You'll want to sort your array of alerts with an NSSortDescriptor (key is whatever key in your objects you want to sort by): 您将需要使用NSSortDescriptor对警报数组进行排序(键是您要作为对象排序依据的键):

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"SENDER_NAME" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:descriptor];
NSArray *sortedArray = [objects sortedArrayUsingDescriptors:sortDescriptors];

Update 更新

Ok, so if you're looking to order it like snapchat does, you'll need a mixed approach here. 好的,因此,如果您要订购像Snapchat一样的产品,则需要使用混合方法。 I'm not going to spell out the whole algorithm, but it would probably be something like this: 我不会详细说明整个算法,但是可能是这样的:

1) create a new array to hold the newly sorted notifications 1)创建一个新数组来保存新排序的通知

2) loop through your objects array and move all unread notifications to the array you created in step one 2)遍历对象数组并将所有未读的通知移至您在第一步中创建的数组

3) sort the array you created in step one by time (see below) 3)按时间对您在第一步中创建的数组进行排序(如下所示)

4) sort the rest of the objects in your objects array by time (see below) 4)按时间对对象数组中的其余对象进行排序(请参见下文)

5) add the objects you just sorted to the new array 5)将刚刚排序的对象添加到新数组

Sorting by time (very similar to original answer): 按时间排序(非常类似于原始答案):

NSSortDescriptor *dateDescriptor = [NSSortDescriptor
                                     sortDescriptorWithKey:@"date" 
                                                 ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:dateDescriptor];
NSArray *sortedEventArray = [nodeEventArray
         sortedArrayUsingDescriptors:sortDescriptors];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM