First off, a big thank you to the SO community. I have learned a great deal. However, I am still an extreme novice w/re to Objective C and thus have a question. Apologies in advance if this is an ignorant question.
I have subclassed NSURLConnection
to fetch my custom objects (myObject
) from my web API. Each object requires 2 calls to the API for completion. The first call is to grab an id property from a list of my Objects. The second call is the to use that id to construct a different URL and populate the rest of the myObject
properties. All is working well but I have a question as to the correctness of my approach for reloading a tableViewsection based on a completion of all of the
myObjectobjects within an
NSMutableArray`.
Here is the method I call after successfully instantiating and fetching all of the incomplete myObjects
and adding them to an NSMutableArray
. messageString
is a property of myObject
that is only available/set on the second network call for each of the instances of myObject
. Thus, I thought I would use it to check for completeness. arrayOfMyObjects
is mutable and contains all of the incomplete myObjects
. MyStoreClass
is just that. A store that handles the creation of the subclassed NSURLConnections
.
- (void)fetchDetails {
void (^completionBlock)(myObject *obj, NSError *err, int statusCode) = ^(myObject *obj, NSError *err, int statusCode) {
if (!err && statusCode == 200) {
NSArray *completionCheckArray = [arrayOfMyObjects filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"messageString = %@", [NSNull null]]];
if ([completionCheckArray count] == 0) {
[[self tableView] reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
}
} else if (!err && statusCode != 200) {
[self statusCodeError:statusCode];
} else {
[self generalError:err];
}
};
for (myObject *mobj in arrayOfMyObjects) {
[[MyStoreClass sharedStore] fetchDetails:mobj withCompletion:completionBlock];
}
}
While this works, it seems inefficient to me to have to create an array through the completion block for every single one of myObjects
. If so, what would be an alternative approach to checking completion of all of myObjects
?