3

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 themyObjectobjects within anNSMutableArray`.

Here is the method I call after successfully instantiating and fetching all of the incomplete myObjects and adding them to an NSMutableArray. messageStringis 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?

Adrian P
  • 6,479
  • 4
  • 38
  • 55
natenash203
  • 699
  • 6
  • 15

1 Answers1

2

A possible and feasible approach would be to go wit KVO (Key Value Observing). Check out the appropriate Key-Value Observing Programming Guide.

For example: If you know how many objects (i.e. object count) you're expecting from a given service call, you could hook up an observer on the array object holding your objects and be notified whenever an element is added etc. See Observing an NSMutableArray for insertion/removal.

Community
  • 1
  • 1
Nenad M
  • 3,055
  • 20
  • 26
  • Thanks for your answer. I pondered this as prior to the second set of calls (for completion of each of myObjects), I do know how many myObjects I will go and fetch. Are you recommending observation of the NSMutableArray (arrayOfMyObjects)? Or the observation of the messageString property for each of the myObjects? – natenash203 Dec 03 '12 at 19:02
  • Yeah, observing your arrayOfMyObjects would be a possible way. Then, whenever a new object is added to your arrayOfMyObjects you could reload your table view and so on. – Nenad M Dec 03 '12 at 19:15
  • I see. I was trying to avoid reloading the tableView multiple times, hence trying to observe completion of *all* instances of myObjects. I will take a look a further look at KVO and see if I can wrap my head around it. – natenash203 Dec 03 '12 at 19:49
  • In your execution logic, when do know when all your data is loaded? Are you working with NSOperation and NSOperationQueue? If so, use KVO to observe the operations property of your queue, then you can tell if your queue has completed by checking for [queue.operations count] == 0. – Nenad M Dec 04 '12 at 07:46
  • I know when an *individual* myObject is complete through a couple means. 1) The completion block is executed from my subclassed NSURLConnection and 2) The messageString property is complete for the instance of myObject. However, I seem to be missing the logic that would put each of the NSURLConnection objects into some sort of group, that I could then observe for completion. I will research the NSOperationQueue and see if that is the right way to go. Thanks for the additional information. – natenash203 Dec 04 '12 at 16:54
  • Thanks @Nenad. Looks like implementing the NSOperationQueue is the way to go, instead of my rather duct tapey approach. – natenash203 Dec 06 '12 at 22:09