LoginSignup
2
1

More than 5 years have passed since last update.

違うコンテキストから生成したモデルインスタンス間でリレーションは作れない

Posted at

違うコンテキストから生成したモデルインスタンス間でリレーションは作れないです。

やろうとすると

Illegal attempt to establish a relationship 'belongs_to' between objects in different contexts

になります。

ところで、appDelegate.swiftに定義されたコンテキストを、
コアデータを使いたい処理で

let appDel : AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context : NSManagedObjectContext
...

のように呼び出すのが嫌だったので

を参考に、シングルトンクラスでコアデータを操作するようのクラスModelUtilを作って、

ModelUtil.sharedInstance.insertObject(..)

のように呼び出すように書いているのですが、
弊害とかあるのでしょうか。

(今回のエラーは、このシングルトンクラスで作成したコンテキストと、appDelegateに残したままだったコンテキストで生成したモデル同士でリレーションを作ろうとしてエラーになりました。)

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1