3 Requests per object with AFIncrementalStore using NSFetchedResultsController

331 Views Asked by At

I have everything setup with AFIncrementalStore to use Parse as a backend service. Using such a service makes counting API requests important as if I exceed 1MM requests/month (though a successful metric!) could result in some decent bills. This usually wouldn't be an issue, but what I'm seeing, if I put a breakpoint at the following method in AFHTTPClient:

- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation;

I find that requests for a particular object occurs 3 times, when I only need it once.

$1 = 0x0b9afe30 https://api.parse.com/1/classes/Poem/cUpTflj0j8
// ...
$8 = 0x099e0a30 https://api.parse.com/1/classes/Poem/cUpTflj0j8
...
$18 = 0x0bb6d530 https://api.parse.com/1/classes/Poem/cUpTflj0j8

Note that the different memory addresses come from AFHTTPRequestOperation, I'm printing the request.URL property to examine the objectId (the random string on the end of URL) from each request.

Now I'm not familiar enough with the workings of NSIncrementalStore or AFIncrementalStore to figure this out after two days of debugging and examination. I'd love to hear any thoughts around what could be causing this, be it intentional or not.

I might add that I'm using a NSFetchedResultsController with a UITableView. I followed the stack trace of the first request of an object to my tableview's data source. The other 2 requests trace back to methods internal to AFIncrementalStore.

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // ...
    NSManagedObject *poem = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [poem valueForKey:@"title"];  // traced back to here
    // ...
}
1

There are 1 best solutions below

0
On

An option may be setting up an NSURLCache like so:

NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:8 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil];

[NSURLCache setSharedURLCache:URLCache];

This should set up an URLCache using 8 MB RAM and 20 MB disk cache.

To check if there are still this many requests happening on the wire, maybe use a tool like wireshark or cocoapacketanalyser.