2013-07-19 4 views

答えて

5

ドキュメントをご覧ください。
https://github.com/magicalpanda/MagicalRecord/blob/master/Docs/Saving-Entities.md

また、過去に質問したときに非常に反応しました。あなたはいつでもそれを手にすることができます。

編集:

なぜ私がダウン票を得たのか分かりません。たぶん、ドキュメントが混乱していたかもしれません。私はMagicalRecordの最新バージョンを使用していないんだけど、私はこれは私が私のエンティティを作成したときに私が始めたとき、私は実現しなかった

//get the context for the current thread 
    //this context can be updated by anyone other process on this thread that uses the same MR_contextForCurrentThread call 
    //it's a local store that can be merged to a parent store 
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread]; 

    //create an NSManagedObject of type YourEntity and insert it into the localContext object 
    NSManagedObject *obj = [YourEntity MR_createInContext:localContext]; 

    //make any updates to obj 

    //save the localContext async 
    //this call should save all nested NSManagedObjectContexts as well (if they exist) 
    [localContext MR_saveToPersistentStoreWithCompletion:^{ 
     //when the async save is complete, this block gets executed 
     //blocks work very similarly to javascript callbacks 
     //basically it's a function reference or block of code that get's packaged up and can be passed around 
     //In this case, the completion block allows to to run any code after the save has been completed. 
    }]; 

一つのことだった正しいはずだと思う

- (void) MR_saveToPersistentStoreWithCompletion:(MRSaveCompletionHandler)completion; 

を使用してみてくださいそれをコンテキストに挿入しました。それは、私が間違って保存する必要のないオブジェクトを保存する原因となりました。これを避けるために、サブコンテキストをセットアップし、オブジェクトを永続化したいときにのみ保存します。

self.context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; 
self.context.parentContext = [NSManagedObjectContext MR_defaultContext]; 
+0

なぜあなたがダウンボートを持っているのかわからないけど、それは私ではなかった(それを否定するためにちょうどアップvoted)。あなたが言及したメソッドを実装するもう1行のコードを提供してもらえますか?私はまだObjective-Cの構文をすべて取得していますが、このメソッドでタイプすると自動完成する^(BOOL Success、NSError * error)完了というwithCompletionを実際に理解していません... – Apollo

+3

FYI - それはありませんでした私はダウン投票しましたが、この回答_did_は、10kユーザーのフラグキューに表示されます。これは、リンクオンリーの回答がStackOverflowのGood Thingとはみなされないためです。リンクが壊れても、あなたの答えはまだ役立つように、常に十分な情報を入れてください。詳細については、[here](http://meta.stackexchange.com/q/8231/164376)を参照してください。 – joran

+1

MR_contextForCurrentThreadは推奨されていません – Gargo

関連する問題