views:

389

answers:

1

This is probably obvious, but I'm a bit of a newbie and have spent hours trying to figure this out.

In viewDidLoad() I have a call to a function on an MGTwitterEngine object (custom object), with an input. If the request (to Twitter, in this case) is successful, it calls an appropriate delegate method.

In viewDidLoad(), it's like this:

[myTwitterEngine getSearchResultsForQuery:@"#quote" sinceID:0 startingAtPage:0 count:10];

The delegate method it calls in the event of a successful search request looks like this:

- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier
{
    NSMutableArray *tempArray = [[[NSMutableArray alloc] init] autorelease]; 

    if ([searchResults count] > 0)
    {
     for (int n=0; n < 15; n++) {

     NSDictionary *singleResult = [searchResults objectAtIndex:n];

     NSString *fromUser = [singleResult valueForKey:@"from_user"];
     NSString *text = [singleResult valueForKey:@"text"]; 

     Message *newMessage = [[Message alloc] initWithUsername:fromUser message:text];

     [tempArray addObject:newMessage];
     }

     self.messages = tempArray;

     // Okay, works here.
     NSLog(@"From delegate method: \n %@", [[self.messages objectAtIndex:13] theMessage]);
    }
}

The thing I can't figure out is this: At the end, where I say "Okay, work's here", it's clear that self.messages (an NSArray) is populated with information I want.

However, self.messages is empty if I call it again in viewDidLoad() after the first method. This is what I've done in viewDidLoad():

[myTwitterEngine getSearchResultsForQuery:@"#quote"];
NSLog(@"\n Before Or After? \n");
NSLog(@"From viewDidLoad: %@", [[self.messages objectAtIndex:13] theMessage]);

The results are strange. The result of the first NSLog comes before the results of the delegate method. Here's the console output:

2009-12-05 23:15:20.758 Entendu[54463:207] 
 Before Or After? 
2009-12-05 23:15:20.761 Entendu[54463:207] From viewDidLoad: (null)
2009-12-05 23:15:21.005 Entendu[54463:207] Request succeeded for connectionIdentifier = 92B2E7EC-AA2F-4301-812E-E8E5AEAF7035
2009-12-05 23:15:21.007 Entendu[54463:207] From delegate method: 
 RT @thankfulnotes: RT @felicelam It usually takes me more than three weeks to prepare a good impromptu speech. Mark Twain #quote

Eventually I want to use the messages Array as a datasource for a table. How might I access this variable from within the delegate method elsewhere in the implementation?

Anyone know why delegate the output from the delegate method comes at the end even though I've called the actual function before the before-or-after NSLog?

Any help appreciated!

A: 

I know nothing of MGTwitterEngine, but clearly -getSearchResultsForQuery: is an asynchronous method; it will return immediately and call your delegate method some time later when results are available.

Your array is empty in -viewDidLoad because the results haven't been received yet. Furthermore, your array will always be empty in -viewDidLoad because your delegate method can't be called until you return control to your run loop.

Darren
Thank you! My approach to this was completely wrong.I had a feeling that viewDidLoad() continued without the data, but didn't know how to eventually update my table with it. In a moment of sudden revelation, I realized I could simply do a [self.tableView reloadData]. And it's done!Again, thank you!
Parimal