应用程序滞后于通过RestKit和Magic Record进行的巨大核心数据映射



我仍在努力找出加载UI线程的内容。在一个类(UITableView的子类)中,有一个FRC:

 NSFetchRequest *request = [DEPlace MR_requestAllWithPredicate:[NSPredicate predicateWithFormat:@"isWorking == YES"]];
 request.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES] ];
 self.placesController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                managedObjectContext:[NSManagedObjectContext MR_rootSavingContext]
                                                                  sectionNameKeyPath:nil
                                                                           cacheName:nil];
 self.placesController.delegate = self;

它曾经附加到MR_contextForCurrentThread。将其更改为rootSavingContext会略微影响性能。然后我将根上下文和默认上下文都设置为同一个:

[NSManagedObjectContext MR_setRootSavingContext:managedObjectStore.persistentStoreManagedObjectContext];
[NSManagedObjectContext MR_setDefaultContext:managedObjectStore.persistentStoreManagedObjectContext];

默认上下文曾设置为mainQueueManagedObjectContext。我想移动与后台相关的所有核心数据,并让FRC负责与UI的交互。FRC代表通过获取新数据

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
  //self.places = [self sortPlaces:controller.fetchedObjects];
  self.places = controller.fetchedObjects;
  [self.delegate contentUpdatedInDatasource:self];
} 

我现在禁用了排序,认为它可能会影响主线程。我试着弄清楚还有什么可以用时间档案器加载主线程,但没有发现任何可疑之处。屏幕截图

当所有数据都加载好后,一切都会顺利运行,应用程序只会在第一次启动时,即数据库填充时滞后。由于所有与加载相关的东西都由RestKit持有,我认为这不会引起问题。

我想最多每秒延迟10次请求,但不知道如何实现。基本上,在启动时,应用程序会获取一个ID数组(到目前为止约为250个),然后在数组中循环,并按每个ID向服务器请求数据。到目前为止,这并不重要,但当数组增加到1-2k时,这将是一个大问题。顺便说一句,一个数据对象在数据库中有4个关系。减少依赖性是一种可能的解决方案吗?

更新:我试图将请求拆分为1乘1,这导致了一种非常奇怪的行为。由于某种原因,请求之间存在巨大的延迟。这就是我如何获得一个ID数组

        AFJSONRequestOperation *op = [[AFJSONRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[APIRoot stringByAppendingFormat:@"/venues/listId?%@=%@&%@=%@", TokenKey, [DEUser token], UDIDKey, [DEUser udid]]]]];
        // dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
        dispatch_queue_t backgroundQueue = dispatch_queue_create("com.name.bgqueue", NULL);
        op.successCallbackQueue = backgroundQueue;
        [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
             //gettin an array of IDs
            NSArray *array = (NSArray*) responseObject;
            if(array.count)
            {
                _array = array;
                [self getVenuesFromSelfArrayWithCurrentIndex:0];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"3rr0r: %@", error);
        }];
        [[NSOperationQueue mainQueue] addOperation:op];

这是一个递归方法的代码:

- (void)getVenuesFromSelfArrayWithCurrentIndex: (NSUInteger)index
{
if(index >= _array.count){ NSLog(@"loading finished!"); return; }
//version of the app, location e.t.c.
NSMutableDictionary *options = [[self options] mutableCopy];
[options setObject:[_array objectAtIndex:index] forKey:@"venueId"];
//method below calls RKs getObjectsAtPath, and it's pretty much the only thing it does
[[DEAPIService sharedInstance] getObjectsOfClass:[DEPlace class]
                                     withOptions:options
                                         success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){
                                             NSManagedObject *object = [mappingResult.array firstObject];
                                             if([object isKindOfClass:[DEPlace class]])
                                             {
                                                 [self getVenuesFromSelfArrayWithCurrentIndex:index+1];
                                             }
                                         } failure:^(RKObjectRequestOperation *operation, NSError *error){
                                            NSLog(@"Failed to load the place with options: %@", options.description);
                                             [self getVenuesFromSelfArrayWithCurrentIndex:index+1];
                                         }];
}

奇怪的是,启动下一个请求大约需要1-2秒(!),cpu使用日志和线程看起来。。奇怪的

屏幕截图1

屏幕截图2

有什么建议吗?

此时我只能建议大约250个请求。如果不淹没网络并在移动设备上使其停止,你就无法发出超过4或5个并发网络请求。实际上,您应该更改web服务设计,以便可以发送批处理请求,因为这对客户端和服务器来说都要高效得多。

无论如何,您可以通过设置对象管理器的operationQueuemaxConcurrentOperationCount来限制并发请求。建议将其设置为4。

最新更新