1

此时我有一个方法调用使用 AFHTTPRequestOperation 从 Web 下载数据,如下所示:

- (void)downloadDataForRegisteredObjects:(BOOL)useUpdatedAtDate {
    NSLog(@"downloadDataForRegisteredObjects");
    NSMutableArray *operations = [NSMutableArray array];

    for (NSString *className in self.registeredClassesToSync) {
        NSDate *mostRecentUpdatedDate = nil;
        if (useUpdatedAtDate) {
            mostRecentUpdatedDate = [self mostRecentUpdatedAtDateForEntityWithName:className]; 
        }
        NSMutableURLRequest *request = [[SDAFParseAPIClient sharedClient] GETRequestForAllRecordsOfClass:className updatedAfterDate:mostRecentUpdatedDate];

        AFHTTPRequestOperation *operation = [[SDAFParseAPIClient sharedClient] HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                // Write JSON files to disk
                [self writeJSONResponse:responseObject toDiskForClassWithName:className];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Request for class %@ failed with error: %@", className, error);
            [[NSNotificationCenter defaultCenter]
             postNotificationName:kSDSyncEngineSyncINCompleteNotificationName
             object:nil];

        }];

        [operations addObject:operation];
    }

    [[SDAFParseAPIClient sharedClient] enqueueBatchOfHTTPRequestOperations:operations progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {

    } completionBlock:^(NSArray *operations) {
        // Process JSON into CD
        if (useUpdatedAtDate) { 
            [self processJSONDataRecordsIntoCoreData]; 
        }

    }];
}

据我了解,我们创建了一个 NSURLMutableRequest,将其传递给带有成功和失败块的 AFHTTPRequestOperation。

成功块说,如果成功,则测试是否字典,如果是,则将其写入磁盘。失败块说,记录错误并发布通知。

该方法在我的应用程序中被连续调用两次,一个接一个。第一次它返回一个空的 responseObject,但第二次它返回一个完整的 responseObject。

为什么会这样?

4

0 回答 0