'+entityForName: nil is not a legal NSManagedObjectContext parameter - Core Data

后端 未结 8 716
梦如初夏
梦如初夏 2020-11-28 22:26

I have added all of the relevant code to the App Delegate, and I am able to add to the data model and fetch from the data model in applicationDidFinishLaunchingWithOptions.<

8条回答
  •  孤城傲影
    2020-11-28 22:59

    I'm a fan of lazy initialization. This way if you need to inject a new context for testing you can, or it'll get it's context from the app delegate if you set up your MOC there.

    class.h
    @property (strong, nonatomic,getter=getManagedObjectContext) NSManagedObjectContext *managedObjectContext;
    
    class.m
        -(NSManagedObjectContext *)getManagedObjectContext {
            if (_managedObjectContext) {
                return _managedObjectContext;
            }
            _managedObjectContext = [[(AppDelegate *)[[UIApplication sharedApplication]delegate]sharedDataModel]managedObjectContext];
            return _managedObjectContext;
        }
    

提交回复
热议问题