Серьезная проблема в разборе XML и хранении деталей с использованием coredata - PullRequest
0 голосов
/ 22 июня 2011

Я использую API погоды Google для получения информации о погоде, я использую coredata для хранения информации. Я сталкиваюсь с troble. Я получаю результаты по меткам и UImageview для хранения информации с использованием coredata. Мой код размещен ниже

-(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/partly_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];  
}   

1 Ответ

0 голосов
/ 22 июня 2011

Вы действительно не описали в чем проблема , но я вижу некоторые возможные проблемы с производительностью в вашем коде ...

Вы повторяете цикл несколько раз:

    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 ];


}

рефакторинг этих циклов в один цикл с результатами:

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 ];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...