2012-04-12 25 views
0

私のアプリケーションには都市名を表すセルのリストを持つUITableViewControllerがあります。ユーザーがセルをクリックすると、アプリケーションはセルaccessoryTypeをUITableViewCellAccessoryCheckmarkに変更し、セル(都市名)のテキスト値を永続ストアに保存します。しかし、私がセルをクリックするたびに、Persistent Storeに実際にデータを格納するのに少なくとも10秒かかることがわかりました。なぜこのようなことが起こるのか?直ちにデータを保存するのではなく、なぜそれが長く続くのはなぜか?NSManagedContextには非常に長い時間がかかります

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cityName = [self.citySettingModel nameOfCityAtIndex:indexPath.row OfCountryAtIndex:self.countryIndex];  
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [[self.citySettingModel worldbikesCoreService] removeCity:cityName]; 
    } 
    else { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     [[self.citySettingModel worldbikesCoreService] addCity:cityName toCountry:self.countryName]; 
    } 
} 

市と国のオブジェクトを作成するためのコードは、別のクラスである

- (City *) addCity:(NSString*) cityName toCountry:(NSString*) countryName 
{ 
    NSManagedObjectContext *context = [self.delegate managedObjectContext]; 

    Country *country = [self.countryDAO addCountry:countryName inManagedObjectContext:context]; 

    City *city = [self.cityDAO addCity:cityName inManagedObjectContext:context]; 

    [country addCitiesObject:city]; 
    return city; 
} 

- (City*) addCity:(NSString*) cityName inManagedObjectContext:(NSManagedObjectContext *)context 
{ 
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"City"]; 
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"cityName = %@", cityName]; 
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"cityName" ascending:YES]; 
    fetchRequest.sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 

    NSError *error; 
    NSArray *matches = [context executeFetchRequest:fetchRequest error:&error]; 

    if (error) NSLog(@"error when retrieving city entities %@", error); 

    City *city; 

    if (!matches || [matches count] > 1) ; 
    else if ([matches count] == 0) { 
     city = [NSEntityDescription insertNewObjectForEntityForName:@"City" inManagedObjectContext:context]; 
     city.cityName = cityName; 
    } 
    else city = [matches lastObject]; 

    return city; 
} 
+0

まず、上記のコードで実際にコンテキストを保存していますか?私はここで貯金が起こっているとは思わない。 Second:addCityのあなたのfetchrequestが... 10秒間責任を負っているとは思わない? – codeclash

答えて

1

取っているかを調べるには本当に良い方法Xcodeでとても長い時間が時間プロファイラである - それはあなたでなければなりませんiOSアプリ開発の親友。

Xcodeでは、[実行]の代わりに[テスト]を選択する必要があります。[タイムプロファイラ]を選択します。どのプロセスにどのくらいの時間がかかるかを確認できます。

コアデータフェッチを調べるツールもあります。

ここで最も時間がかかっていて、投稿を更新してください。または、恐らくあなた自身がそれを解決してくれるかもしれません)

関連する問題