实现时间敏感的调度队列的最佳方法

时间:2016-04-07 23:37:57

标签: ios objective-c multithreading optimization queue

所以我有一个服务,我可以通过传递请求体中的profileID来请求用户的profile-json。如果我需要10个人的个人资料,我会传入10个profileID,服务器将返回所有10个个人资料信息。最大数量= 20。

所以我添加了一个优化算法,它基于用户滚动非常快速地组合了一堆profileID,或者用户在应用程序中打开了一堆配置文件等等......基本上我的逻辑是我有一个大致等待的dispatch_queue 0.6秒,在那段时间我有一个数组,收集应用程序将要求的所有profileIDs不同的视图。在0.6秒之后,无论在阵列中排队的是什么,我都将其包含在请求体中,并将​​其发送到服务器。

我可以在这里使用哪些更好的策略/设计模式?

我的实现看起来像这样。

 ----> call in the main thread <--------



 CXAssert([NSThread isMainThread], @"profileInfoForUsers should be main thread");


        if (!profileIDCacheWaitingToRequest) {
            profileIDCacheWaitingToRequest = [NSMutableArray array];
        }

        if (!profileIDArraySubmittedToTheServer) {
            profileIDArraySubmittedToTheServer = [NSMutableArray array];
        }
        if (!_profile_batch_process_queue) {
            _profile_batch_process_queue = dispatch_queue_create("com.gather.chatx.profile.batch.process", DISPATCH_QUEUE_SERIAL);
        }

        NSMutableArray *filteredProfileIDCache = [NSMutableArray array];
        for (NSString *profileID in users) {
            if (profileID == NULL) {
                continue;
            }

            if (![profileIDArraySubmittedToTheServer containsObject:profileID]) {
                CXDebugLog(@"Not sent to server yet");
                if (![profileIDCacheWaitingToRequest containsObject:profileID]) {
                    CXDebugLog(@"Not added to queue yet yet");
                    [filteredProfileIDCache addObject:[profileID copy]];
                } else {
                    CXDebugLog(@"Already present in the queue, will fire soon");
                }
            } else {
                CXDebugLog(@"Skipping profile download call for %@", profileID);
            }
        }

        if (filteredProfileIDCache.count == 0) {
            CXDebugLog(@"No need for submitting profile to server as we are already waiting on server to return..");
            completion (nil, nil);
            return;
        }

        for (NSString *pid in filteredProfileIDCache) {
            if (profileIDCacheWaitingToRequest.count <= GROUP_PROFILE_MAX_COUNT) {
                [profileIDCacheWaitingToRequest addObject:pid];
            } else {
                CXDebugLog(@"wait next turn. hope the user makes this call.");
                continue;
            }
        }

        CXProdLog(@"Queuing profiles for download...");
        dispatch_async(_profile_batch_process_queue, ^{
            // this block will keep executing once its a serial queue..

            /////// SLEEP this thread, and wait for 0.6 seconds


            sleep(0.6);


            dispatch_async(dispatch_get_main_queue(), ^{

                if (profileIDCacheWaitingToRequest.count == 0) {
                    completion (nil, nil);
                    return ;
                }

                NSArray *profileIDsToRequest = [profileIDCacheWaitingToRequest copy];
                [profileIDArraySubmittedToTheServer addObjectsFromArray:profileIDsToRequest];

                CXProdLog(@"Fetching profiles from server with count %lu", (unsigned long)profileIDsToRequest.count);

                if (profileIDsToRequest.count > GROUP_PROFILE_MAX_COUNT) {
                    CXDebugLog(@"We are exceeding the count here");
                    NSMutableArray *array = [NSMutableArray arrayWithArray:profileIDsToRequest];
                    NSUInteger totalNumberOfObjectsToRemove = profileIDsToRequest.count - GROUP_PROFILE_MAX_COUNT;

                    for (int i = 0; i < totalNumberOfObjectsToRemove; i ++) {
                        [array removeLastObject];
                    }

                    profileIDsToRequest = array;
                }

                [CXDownloader downloadProfileForUsers:profileIDsToRequest sessionKey:self.sessionKey completion:^(NSDictionary *profileData, NSError *error) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [profileIDArraySubmittedToTheServer removeAllObjects];
                        if (profileData) {
                           ....
                        }
                        else {
                            ....
                        }

                    });
                }];

                [profileIDCacheWaitingToRequest removeAllObjects];
            });

        });

0 个答案:

没有答案