내 응용 프로그램에 도시 이름을 나타내는 셀 목록이있는 UITableViewController가 있습니다. 사용자가 셀을 클릭하면 응용 프로그램에서 셀 accessoryType을 UITableViewCellAccessoryCheckmark로 변경하고 셀 (도시 이름)의 텍스트 값을 지속 된 저장소에 저장합니다. 그러나 셀을 클릭 할 때마다 영구 저장소에 실제로 데이터를 저장하는 데 적어도 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;
}
첫 번째 : 실제로 위의 코드에서 컨텍스트를 실제로 저장합니까? 여기 저축은 일어나지 않습니다. Second : addCity에서의 fetchrequest가 ... 10 초 동안 책임이 있다고 생각하지 않습니까? – codeclash