私はiPhoneアプリを書いています。データを永続ストレージに保存する必要がありますが、問題が発生しています... xcdatamodelでエンティティを作成しましたが、属性にデフォルト値を設定しても問題ありません。私のテストでは、以下のコードを数回実行し、アプリケーションがアクティブなときにこの属性(整数)を変更できます。ただし、アプリを閉じて再度開くと、整数(myInt)は、更新した最後の値であると予想したときにデフォルト値に戻ります。これが私がしていることです:
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSEntityDescription *myTestEntity = [NSEntityDescription
entityForName:@"TestEntity"
inManagedObjectContext:managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:timeStampEntity];
NSError *error = nil;
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
// TODO: Handle the error
}
for (id oneObject in fetchedObjects)
{
NSInteger myInt = [[oneObject valueForKey:@"myStoredInt"]intValue];
myInt++;
[oneObject setValue:[NSNumber numberWithInt: myInt] forKey:@"myStoredInt"];
NSError *error = nil;
if (![managedObjectContext save:&error]) {//TODO: Handle error
}
}
[fetchRequest release];
私はその行を望んでいました:
if (![managedObjectContext save:&error])
仕事をして永続ストレージに変更を保存しますが、そうではありません!(この行をforループの内側または外側に配置しても、結果は変わりません)何が間違っていますか?
ご助力ありがとうございます!