0

こちらで質問するのは初めてで、申し訳ありませんが、同様の質問が見つかりません。したがって、エンティティ「市」属性 - @「名前」のデータを更新する必要があります。たとえば、コア データには、@"New York"、@"Boston" が既にあります。XML を解析すると、NSMutableArray *Cities = (@"New York", @"Boston", @"Los Angeles", @"Washington"); が得られます。

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSString *attributeString = @"name";
    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
//save to the TableView
cell.textLabel.text = [[object valueForKey:attributeString] description];
if ((indexPath.row + 1) == numberOfSectionsInTableView && (self.isParsingDone))
        [self.insertNewObjectToCities:nil];
//When coredata updating - tableView is also updating automatically


//Here is just adding new data, but I do not know how to update
- (void)insertNewObjectToCities_translation:(id)sender
{
     NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
NSString *attributeString = @"name";

if (![[self.parseCities.Cities objectAtIndex:i] isEqualToString:[newManagedObject valueForKey:attributeString]])
{
    [newManagedObject setValue:[self.parseCities.Cities objectAtIndex:i] forKey:attributeString];
    NSLog(@"OBBB %@", [self.parseCities.Cities objectAtIndex:i]);
    NSLog(@"dessss %@", [[newManagedObject valueForKey:attributeString] description]);

    i++;

    if (i==[self.parseCities.Cities count]) 
    {
        i = 0;
        return;
    }
    else 
    {
        NSLog(@"valueForKey %@", [newManagedObject valueForKey:attributeString]);
        [self insertNewObjectToCities_translation:nil];
    }
}
else 
{
    NSLog(@"else");
    return;
}

// Save the context.
NSError *error = nil;
if (![context save:&error]) {
    // Replace this implementation with code to handle the error appropriately.
    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

}
4

1 に答える 1

1

管理対象オブジェクトを更新するには、まずそれを取得し、取得した のフィールドに変更を加えてからNSManagedObject、オブジェクトの取得に使用したコンテキストを保存する必要があります。もう一度呼び出すinsertNewObjectForEntityForNameと、Core Data に既に存在する場合でも、毎回新しい管理対象オブジェクトが挿入されます。

新しいオブジェクトを追加する必要があるかどうかを確認する必要があるたびに、1 つのオブジェクトを取得するのは非常に時間がかかります。代わりに、現在ロードしているオブジェクト (またはそれらの一意の識別フィールド) を or にキャッシュしてNSArrayNSSetメンバーシップを確認できるようにすることができます。

于 2012-05-14T17:17:04.193 に答える