开发者

NSArray is empty when I call it from another class (xcode)

开发者 https://www.devze.com 2023-03-27 04:37 出处:网络
I\'m kind of a newbie in objective-c and I\'m having issues when I call a NSArray from other class. I have a class which handles the parsing of a XML feed and another to manage the UItableview stuff.

I'm kind of a newbie in objective-c and I'm having issues when I call a NSArray from other class. I have a class which handles the parsing of a XML feed and another to manage the UItableview stuff. It's weird because when it's done synchronously (using NSXMLParser methods) all data is shown in the table, but when I use the NSURLConnection to make it asynchronous it parses all the data but the array is empty when it's 开发者_JAVA百科called. If I call NSLog it shows all data contained the newsStories array when the data is being parsed, but somehow its deleted when I call it.

On the parser class I have and all the methods of the NSXMLParser:

- (void)parseXMLFileAtUrl:(NSString *)URL {
     data = [[NSMutableData alloc] init];
     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
     NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
     if (connection) {
         data = [[NSMutableData alloc]init];
     }
     [connection release];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //Reset the data as this could be fired if a redirect or other response occurs
    [data setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)_data
{
    //Append the received data each time this is called
    [data appendData:_data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //Start the XML parser with the delegate pointing at the current object
    _parser = [[NSXMLParser alloc] initWithData:data];
    newsStories = [[NSMutableArray alloc] init];
    [_parser setDelegate:self];
    [_parser setShouldProcessNamespaces:NO];
    [_parser setShouldReportNamespacePrefixes:NO];
    [_parser setShouldResolveExternalEntities:NO];
    [_parser parse];
}

And this is how I call the array:

-(BOOL) loadData{
    NSString *latestUrl = [[NSString alloc] initWithString:feed];
    if ([latestArray count] == 0) {
        news = [[news_parser alloc]init]; 
        [news parseXMLFileAtUrl:latestUrl];
        [self setArray:news.newsStories];--- here it says null for Array and for newsItems
    }
    [latestUrl release];
    return YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    array = [[NSMutableArray alloc]init];
    _News.rowHeight =85;
    [self loadData];
    [self._News reloadData];    
}

Any help would be appreciated, thanks guys! Regards.


... Do you understand what asynchronous means? It means that your function will return and the connection will continue, making the callbacks when it is ready. The way you have this coded up, you start the connection and then immediately try to use the data -- it isn't there yet! You need to wait until after connectionDidFinishLoading before you try to use the array.

Do some more research on what exactly it means to be asynchronous; it seems you're not understanding that.

Edit

Let me clarify, since you seem to have missed my point. Your viewDidLoad function finishes long before your connectionDidFinishLoading callback gets called, and so of course the newsStories array is not there yet. When you call:

[news parseXMLFileAtUrl:latestUrl];

in the loadData function, that doesn't stop and wait for the connection to return; if it were synchronous, it would, but asynchronous does not. (Hence I ask you to research what asynchronous actually means, which apparently you still have not done). Since that call returns and then you immediately try to use the loaded data (long before the connectionDidFinishLoading is called) you naturally don't have any data in there.


From Apple's docs:

Mutable objects are generally not thread-safe. To use mutable objects in a threaded application, the application must synchronize access to them using locks. (For more information, see “Atomic Operations”). In general, the collection classes (for example, NSMutableArray, NSMutableDictionary) are not thread-safe when mutations are concerned. That is, if one or more threads are changing the same array, problems can occur. You must lock around spots where reads and writes occur to assure thread safety.

Reading this might help you out. Not totally sure if that is what is going on in your app but it looks like a good place to start.

0

精彩评论

暂无评论...
验证码 换一张
取 消