-1

I don't know why this code error. Please help. I read some articles and I think the problem is about context. What Should I do?

This program is about shows data in coredata to label in viewcontroller.

AppDelegate.h    
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

@end

AppDelegate.m
#import "AppDelegate.h"
#import "Test.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

NSManagedObjectContext *context = [self managedObjectContext];
Test *t = [NSEntityDescription insertNewObjectForEntityForName:@"Test"
                                        inManagedObjectContext:context];
t.name = @"please";



return YES;}

ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *label;
@property (nonatomic,strong) NSArray *temp;
@property (nonatomic,strong) NSManagedObjectContext* managedObjectContext;
@end

ViewController.m
#import "ViewController.h"
#import "Test.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize managedObjectContext;
@synthesize temp;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription
                                    entityForName:@"Test"  
                       inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    NSError *error;
self.temp = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
for(Test *info in temp){

    _label.text = info.name;
}

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

Test.h    
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface Test : NSManagedObject

@property (nonatomic, retain) NSString * name;

@end

Test.m
#import "Test.h"


@implementation Test

@dynamic name;  
@end

I don't know why this code error. Please help. I read some articles and I think the problem is about context. What Should I do?

Newbie
  • 1
  • Could you elaborate on where the error occurs? Also, please try to remove all unnecessary code (ex: `didReceiveMemoryWarnimg`) as it doesn't help us getting quickly to the point. – Romain Mar 02 '15 at 19:29
  • It error on NSEntityDescription *entity = [NSEntityDescription entityForName:@"Test" inManagedObjectContext:managedObjectContext]; [fetchRequest setEntity:entity]; – Newbie Mar 02 '15 at 19:34
  • I think because ViewController doesn't know Context From AppDelegate. – Newbie Mar 02 '15 at 19:35
  • How to pass context form AppDelegate to ViewController is the point.I think. – Newbie Mar 02 '15 at 19:36
  • Instead of passing the context on you could try creating a singleton holding the context. This has it's drawbacks, but if you aren't doing anything fancy it might work for you. – Newyork167 Mar 02 '15 at 19:55

2 Answers2

0

In your view controller, replace the line:

@synthesize managedObjectContext;

With this:

- (NSManagedObjectContext *) managedObjectContext {
    return ((AppDelegate *)[[UIApplication sharedApplication] delegate]).managedObjectContext;
}

Instead of storing another, different object context in your view controller, this property will return the object context that you set up in the app delegate.

There are other ways to do this, such as creating a Core Data helper class following the Singleton pattern (as @NewYork167 suggests), but this should at least solve your current problem.

Romain
  • 3,718
  • 3
  • 32
  • 48
0

For any future reference, you can also try subclassing the NSManagedObjectContext like this:

@interface MyManagedObjectContext : NSManagedObjectContext

+ (MyManagedObjectContext *)mainThreadContext;

@end

@implementation MyManagedObjectContext

+ (MyManagedObjectContext *)mainThreadContext;
{
   static MyManagedObjectContext *moc;
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
       moc = [[self alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
       // Setup persistent store coordinator here
   });
   return moc;
}

@end

Reference: Best practices for passing NSManagedObjectContext around to UITabBarController child view controllers?

Community
  • 1
  • 1
Newyork167
  • 494
  • 5
  • 9