は、あなたは、単にフィールドを更新する必要があるものは何でもNSFetchRequest
、変更を(簡単なmyObject.propertyNameセッターはすべてのことが必要なのである)を使用して、既存のオブジェクトを要求し、その後データコンテキスト上の保存アクションを実行します。
EDITにコード例を追加してください。私はMCannonに同意します。
このコードでは、アプリケーションデリゲートに管理オブジェクトコンテキストなどがあるように、コアデータを含むテンプレートを使用してプロジェクトを作成したことを前提としています。ここでエラーをチェックするのは基本的なコードです。
オブジェクト
// Retrieve the context if (managedObjectContext == nil) { managedObjectContext = [(YourAppNameAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; } // Retrieve the entity from the local store -- much like a table in a database NSEntityDescription *entity = [NSEntityDescription entityForName:@"YourEntityName" inManagedObjectContext:managedObjectContext]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:entity]; // Set the predicate -- much like a WHERE statement in a SQL database NSPredicate *predicate = [NSPredicate predicateWithFormat:@"YourIdentifyingObjectProperty == %@", yourIdentifyingQualifier]; [request setPredicate:predicate]; // Set the sorting -- mandatory, even if you're fetching a single record/object NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"yourIdentifyingQualifier" ascending:YES]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [request setSortDescriptors:sortDescriptors]; [sortDescriptors release]; sortDescriptors = nil; [sortDescriptor release]; sortDescriptor = nil; // Request the data -- NOTE, this assumes only one match, that // yourIdentifyingQualifier is unique. It just grabs the first object in the array. YourEntityName *thisYourEntityName = [[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:0]; [request release]; request = nil;
更新
をフェッチオブジェクト
thisYourEntityName.ExampleNSStringAttributeName = @"The new value";
thisYourEntityName.ExampleNSDateAttributeName = [NSDate date];
変更を保存し
NSError *error;
[self.managedObjectContext save:&error];
これで、オブジェクト/行が更新されました。
よろしくお願いします。コードサンプルをお願いしますか?申し訳ありませんが、私はCoreDataにかなり新しいが、サンプルを持っていることは本当に私を理解するのに役立つでしょう。 –
私はいくつかの基本的なコードを含むように私の答えを編集しました。 –
私は確かに読んで、助けてくれてありがとう。しかし、いくつかのことがあります:フェッチ要求は、それがpre - made( 'NSFetchedResultsController *)fetchedResultsController'を置き換えますか?また、 'YourEntityName * thisYourEntityName'は私を混乱させています、実際のエンティティ名ですか?ただエラーが出ます。そして、チャンクを更新して保存すると、フェッチコードがそのまま残っていますか? –