如何使用Magical Record创建&更新对象并保存它们而不使用contextForCurrentThread [英] How do I use Magical Record to create & update objects and save them without using contextForCurrentThread

查看:321
本文介绍了如何使用Magical Record创建&更新对象并保存它们而不使用contextForCurrentThread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了MagicalRecord的博客文章作者:为什么要使用contextForCurrentThread在MagicalRecord中无效



contextForCurrentThread 已弃用, saveWithBlock应该使用,因为它为相关线程创建了一个安全的新< NSManagedObjectContext



我已经使用 contextForCurrentThread 广泛地在我的应用程序到目前为止。但是,我无法找出如何使用 saveWithBlock ,因为我的提取和保存不一定会顺序发生。



目前我正在做的事情:



  localContext = NSManagedObjectContext.MR_contextForCurrentThread 
person = Person .MR_createInContext(localContext)
person.name =John Smith



浏览应用程序,不同的控制器,视图等。其他对象可以使用与上述代码类似的方法创建。



然后在将来的某个任意点,当用户决定保存时,我运行这个方法:

  localContext = NSManagedObjectContext.MR_contextForCurrentThread 
localContext.MR_saveToPersistentStoreWithCompletion(
lambda {| success,error |
#...
}

更新对象,然后保存它们而不使用 contextForCurrentThread

解决方案

我使用目标C而不是RubyMotion,但你应该能够做这样的事情:

  MagicalRecord.saveWithBlock b lambda {| localContext | 
person = Person.MR_createInContext(localContext)
#update person stuff here
}

pre>

EDIT



如果您想稍后保存上下文需要持有它:

  //你的ViewController中的某个地方
NSManagedObjectContext * context = [NSManagedObjectContext MR_confinementContext ];


//其他地方
Person * p = [Person MR_createInContext:context];

//另一种方法
[context MR_saveToPersistentStoreAndWait];

这里的主要思想是你只需要坚持上下文并执行操作当你准备好了。如果您想要进行后台保存,您可以使用以下方法:

  [context MR_saveToPersistentStoreCompletion:^(BOOL success,NSError * error){
//保存完成时在主线程上调用
}];


I just read the author of MagicalRecord's blog post on Why contextForCurrentThread Doesn't work in MagicalRecord.

contextForCurrentThread is deprecated and saveWithBlock should be used instead because it creates a safe new NSManagedObjectContext for the relevant thread.

I've been using contextForCurrentThread extensively in my app so far. However, I'm having trouble figuring out how to use saveWithBlock instead as my fetching and saving are not necessarily happening sequentially.

Currently I'm doing things like:

localContext = NSManagedObjectContext.MR_contextForCurrentThread
person = Person.MR_createInContext(localContext)
person.name = "John Smith"

The user may then browse around the app, different controllers, views etc. are displayed. Other objects may be created using a similar method to the code above.

Then at some arbitrary point in the future, when the user decides to save, I run this method:

localContext = NSManagedObjectContext.MR_contextForCurrentThread
localContext.MR_saveToPersistentStoreWithCompletion(
  lambda { |success, error|
    # ...
  }
)

What is the recommended way to create & update objects and then save them without using contextForCurrentThread?

解决方案

So, I use objective C as opposed to RubyMotion, but you should be able todo things like this:

MagicalRecord.saveWithBlock(
   lambda { |localContext|
       person = Person.MR_createInContext(localContext)
       #update person stuff here
   }
)

EDIT

If you want to save the context later, you just need to hold on to it:

// Somewhere in your ViewController, etc
NSManagedObjectContext *context = [NSManagedObjectContext MR_confinementContext];


// Somewhere else
Person *p = [Person MR_createInContext:context];

// And in yet another method
[context MR_saveToPersistentStoreAndWait];

The main idea here is that you just need to hold on to the context and perform your operations on it when you're ready. If you want a background save to occur, you an use the following method:

[context MR_saveToPersistentStoreCompletion:^(BOOL success, NSError *error){
   //called on the main thread when save is complete
}];

这篇关于如何使用Magical Record创建&amp;更新对象并保存它们而不使用contextForCurrentThread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆