联系人排序类似于viber iOS App

时间:2014-08-26 12:15:00

标签: ios objective-c sorting filtering

我试图过滤类似于Viber iOS App的联系人。通过切换分段控件,将联系人分为两个类别,即“全部”和“仅限App用户”。

在获取联系人列表时,如果联系人数量很高,则需要更多时间。

目前我手动运行for循环来获取联系人,如果有其他替代方法,请告诉我。

`-(void)usersList:(NSMutableDictionary *) dictUsrs    
{
NSDictionary *contactDetails;
arrContactsUser = [[NSMutableArray alloc] init];
[arrContactsUser removeAllObjects];
NSArray *sorted = [[sectionForRecievedContacts allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    //             NSOrderedAscending, NSOrderedSame, NSOrderedDescending
    BOOL isPunct1 = [[NSCharacterSet punctuationCharacterSet] characterIsMember:[(NSString*)obj1 characterAtIndex:0]] || [[NSCharacterSet symbolCharacterSet] characterIsMember:[(NSString*)obj1 characterAtIndex:0]] || [[NSCharacterSet illegalCharacterSet] characterIsMember:[(NSString*)obj1 characterAtIndex:0]] ||
    [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[(NSString*)obj1 characterAtIndex:0]];
    BOOL isPunct2 = [[NSCharacterSet punctuationCharacterSet] characterIsMember:[(NSString*)obj2 characterAtIndex:0]] || [[NSCharacterSet symbolCharacterSet] characterIsMember:[(NSString*)obj2 characterAtIndex:0]] || [[NSCharacterSet illegalCharacterSet] characterIsMember:[(NSString*)obj2 characterAtIndex:0]]||
    [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[(NSString*)obj2 characterAtIndex:0]];

    if (isPunct1 && !isPunct2)
        return NSOrderedDescending;
    else if (!isPunct1 && isPunct2)
        return NSOrderedAscending;

    return [(NSString*)obj1 compare:obj2 options:NSDiacriticInsensitiveSearch|NSCaseInsensitiveSearch];
}];

for (int j=0; j<[[dictUsrs allKeys] count]; j++) {

    for (int i=0; i<[[dictUsrs valueForKey:[sorted objectAtIndex:j]] count]; i++) {

        contactDetails = [[dictUsrs valueForKey:[sorted objectAtIndex:j]] objectAtIndex:i];

        fetchCount++;

        if ([contactDetails objectForKey:@"status"] == nil) {
            NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:j];
                           if (fetchCount < kFetchLimit) {
                                [self startStatusFetcher:contactDetails forIndexPath:path withTableView:tableViewIngIngUsers];
                    NSLog(@"Fetch for Cell For Row at IndexPath");
            }

        }
        if ([contactDetails objectForKey:@"registered"] != nil && [[contactDetails objectForKey:@"registered"] isEqualToString:@"yes"]) {
            [arrContactsUser addObject:contactDetails];

        }

    }

}
   [self.tableViewIngIngUsers reloadData];

} `

2 个答案:

答案 0 :(得分:1)

    `dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

});`

调度队列给了我临时解决方案,以避免这种长时间的提取延迟。

答案 1 :(得分:0)

在另一个线程中获取您的联系人信息,或者只是在应用程序启动时延迟调用。因此,当您想要使用数据时,可以缓存数据,只需读取缓存数据。