开发者

Serious issue in parsing xml and storing details using coredata

I am using google weather api to get weather information,I am using coredata to store information.I am running into troble.I am getting results on labels and UImageview to store information using coredata.My code is posted below

-(IBAction)sBarpress:(id)sender
{

    NSString *urlString = [NSString stringWithFormat:@"http://www.google.co.uk/ig/api?weather=%@",sBar.text];
    NSURL *url = [NSURL URLWithString:urlString];
    NSLog(@"buttonpress");

    WeatherXMLParser *delegate = [[WeatherXMLParser alloc] init];


    NSXMLParser *locationParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
       [locationParser setDelegate:delegate];
    [locationParser setShouldResolveExternalEntities:YES];

    [locationParser parse];
    /*
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:@"Weather" inManagedObjectContext:context];
    request.predicate = [NSPredicate predicateWithFormat:@"uniqueId = %@", [Data objectForKey:@"id"]];

    NSError *error = nil;
    NSManagedContext *returnedData = [[context executeFetchRequest:request error:&error] lastObject];
    [request release];
    [NSEntityDescription insertNewObjectForEntityForName:@"condition" inManagedObjectContext:context];
    MODEL_OBJECT.uniqueId = [Data objectForKey:@"id"];
    MODEL_OBJECT.title = [Data objectForKey:@"title"];
*/

    for (WeatherCondition *condition in delegate.forecastConditions) {
        NSLog(@"description is %@", condition.description);
        hightemplabel.text=condition.description;

            }

    for (WeatherCondition *condition in delegate.forecastConditions) {
        NSLog(@"description is %@", condition.icon);
        NSData *mydata=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.co.uk/ig/images/weather/pa开发者_高级运维rtly_cloudy.gif"]];

        conditionsImageView.image  = [[UIImage alloc] initWithData:mydata ];


    }
    for (WeatherCondition *condition in delegate.forecastConditions) {
        NSLog(@"description is %@", condition.icon);
        NSData *mydata=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.co.uk/ig/images/weather/thunderstorm.gif"]];

        forecastimage.image  = [[UIImage alloc] initWithData:mydata ];






    }
    for (WeatherCondition *condition in delegate.forecastConditions) {
        NSLog(@"description is %@", condition.description);
        humiditylabel.text=condition.description;

    }








    [self fetchRecords];



        [locationParser release];
        [delegate release];


//    NSDictionary *airport = [responseString JSso ];
//    displaybox.text=[airport objectForKey:@"location"];
/*
    NSURL *url=[NSURL URLWithString:@"http://airportcode.riobard.com/airport/%@?fmt=json"];http://free.worldweatheronline.com/feed/weather.ashx?q=omaha%2c+ne,united+states&format=json&num_of_days=5&key=691607e82d192404111506 &format=jason
    //NSURL *url = [NSURL URLWithString:urlString];


    NSData *data = [response dataUsingEncoding:NSUTF8StringEncoding];

    WeatherParser *locationParser = [[WeatherParser alloc] init];
    [locationParser parseData:data];

    label.text = locationParser.location;

    urlString = [NSString stringWithFormat:@"http://free.worldweatheronline.com/feed/weather.ashx?q=%@&format=json&num_of_days=5&key=691607e82d192404111506",locationParser.location];
    url = [NSURL URLWithString:urlString];
    NSLog(@"area coming");
    [response release];
    response = [[NSString alloc] initWithContentsOfURL:url];

    data = [response dataUsingEncoding:NSUTF8StringEncoding];

    WeatherParser *weatherParser = [[WeatherParser alloc] init];
    [weatherParser parseData:data];

    [locationParser release];
    [weatherParser release];

    */
}

- (void)fetchRecords {   

    // Define our table/entity to use  
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Weather" inManagedObjectContext:managedObjectContext];   
    NSLog(@" entity is %@",entity);
    // Setup the fetch request  
    NSFetchRequest *request = [[NSFetchRequest alloc] init];  
    [request setEntity:entity];   

    // If a predicate was passed, pass it to the query
    if(predicate != nil)
    {
        [request setPredicate:predicate];
    }
    // Define how we will sort the records  
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"condition" ascending:NO];  
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];  
    [request setSortDescriptors:sortDescriptors];  
    [sortDescriptor release];   




    // Fetch the records and handle an error  
    NSError *error;  
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];   

    if (!mutableFetchResults) {  
        // Handle the error.  
        // This is a serious error and should advise the user to restart the application  
    }   

    // Save our fetched data to an array  
    [self setEventArray: mutableFetchResults];  
    [mutableFetchResults release];  
    [request release];  
}   


You didnt really describe what the trouble is, but I can see some possible performance issues in your code ...

You are looping multiple times:

    for (WeatherCondition *condition in delegate.forecastConditions) {
       NSLog(@"description is %@", condition.description);
       hightemplabel.text=condition.description;

        }

for (WeatherCondition *condition in delegate.forecastConditions) {
    NSLog(@"description is %@", condition.icon);
    NSData *mydata=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.co.uk/ig/images/weather/partly_cloudy.gif"]];

    conditionsImageView.image  = [[UIImage alloc] initWithData:mydata ];


}

refactor these loops into one loop through the results:

for (WeatherCondition *condition in delegate.forecastConditions) {
    NSLog(@"description is %@", condition.description);
    hightemplabel.text=condition.description;

    NSLog(@"icon is %@", condition.icon);
    NSData *mydata=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.co.uk/ig/images/weather/partly_cloudy.gif"]];

    conditionsImageView.image  = [[UIImage alloc] initWithData:mydata ];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜