2012-04-12 2 views
0

내 응용 프로그램에 도시 이름을 나타내는 셀 목록이있는 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; 
} 
+0

첫 번째 : 실제로 위의 코드에서 컨텍스트를 실제로 저장합니까? 여기 저축은 일어나지 않습니다. Second : addCity에서의 fetchrequest가 ... 10 초 동안 책임이 있다고 생각하지 않습니까? – codeclash

답변

1

엑스 코드에서 너무 오래 시간을내어 무엇을 찾을 수있는 정말 좋은 방법에 대한 코드는 시간 프로파일 - 그것은해야 당신의 iOS 앱 개발에서 가장 친한 친구.

Xcode에서는 Run 대신에 Test를 눌러야합니다. Time Profiler를 선택하십시오. 프로세스에 소요되는 시간을 확인할 수 있습니다.

핵심 데이터 가져 오기를 검사 할 수있는 몇 가지 도구가 있습니다.

보기에 시간이 오래 걸리고 게시물이 업데이트됩니다. 아니면, 어쩌면 당신은 스스로 해결할 수 있습니다.)

관련 문제