views:

41

answers:

1

I have a ViewController that initializes another class that loads data into a mutable array and saves it as a property on itself.

here is my ViewController init code:

-(id) initWithCollectionID:(NSString *)aCollectionID {
    if (self = [super init]){
        collectionID=aCollectionID;
        dataSource = [[CollectionListDataSource alloc] initWithCollectionID:collectionID];
    }
    return self;
}

once dataSource has loaded all of the data into it's property dataSource.collectionItems I set dataSource.loaded = @"true";

how do I use addObserver to watch that value and fire off a function in my ViewController?

something like this I'd assume:

[self addObserver:dataSource forKeyPath:@"loaded" options:NSKeyValueChangeNewKey context:nil];

Then what do I do?

A: 

As your code stands now, it will pause until the data is loaded regardless of whether you use notifications or not. It will not progress past:

dataSource = [[CollectionListDataSource alloc] initWithCollectionID:collectionID];

...until the CollectionListDataSource object has completed its own initialization (which I presume also means the loading its data) and returns an instance of itself.

If you want the CollectionListDataSource object to load while the view controller keeps on initializing, you will need to put the CollectionListDataSource object on another thread. However, you can't have an attribute object running on separate thread.

You seldom need to jump through such hoops. Unless this array is very large (10k+ objects) you most likely don't have to worry about. In most cases, you need the data before the view can function anyway so there's no point in letting the view go on without the data.

If you do need actually need to observe an attribute of another object, see Key-Value Programming Guide: Registering For Key-Value Observing for details.

TechZen
it's talks to a web service using NSURLRequest which is non blocking so it actually doesn't get paused at all, that's the problem I want to view to load and then get refreshed once I have the data.
Slee
Your going to have to park the CollectionListDataSource somewhere else and/or have it load data in another method other than init. Right now, your code will stop initializing the view until the data is load regardless. You'll want to load data in a background thread an notify the view controller when the thread is done.
TechZen
Try this example. It's core data but the principle is the same: http://developer.apple.com/iphone/library/samplecode/TopSongs/Listings/ReadMe_txt.html#//apple_ref/doc/uid/DTS40008925-ReadMe_txt-DontLinkElementID_18
TechZen