2013-03-24 29 views
0

我认为我创建代码来做DO STUFF并不坏。但是我很难存储我的变量和持久数据。我正在使用ARC和故事板(考虑放弃故事板,因为它们似乎在引擎盖下做我没看到的东西)。了解在核心数据中使用NSManagedObjectContext

我请求帮助。我无法理解如何从我发现的资源中获得所需的概念,直到现在。

我的想法是: 用户将看到tableviewcontroller类型的viewController,它为每行显示一个字符串(股票报价名称)。这些字符串需要持久存储,我假设在coredata。该应用程序允许用户在第二个控制器中添加字符串。这些也需要持续保存在第一个数据中。然后,应用程序需要获取一些基于Web的数据,并将其与字符串(股票价格)一起显示。当应用程序退出时,它可以删除基于Web的数据并只保留字符串(股票报价名称)。

我要求帮忙。

该应用程序显示没有警告和编译没有任何问题。

然后它崩溃之后NSLog(@“2”);在下面的代码viewDidLoad,与*终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因是:“+ entityForName:无不是合法的NSManagedObjectContext参数搜索实体名称‘股票’”

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

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

Appdelegate.m 
@synthesize managedObjectContext = _managedObjectContext; 
@synthesize managedObjectModel = _managedObjectModel; 
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator; 

- (void)prvniSpusteni 
{ 
    NSLog(@"Firt run of app, example Stocks added."); 
    NSManagedObjectContext *context = [self managedObjectContext]; 

    // Creating, Apple, Google, Bekrshire B 

    Stock *akcie1 = [NSEntityDescription insertNewObjectForEntityForName:@"Stock" inManagedObjectContext:context]; 
    akcie1.ticker = @"AAPL"; 
    akcie1.index = [NSNumber numberWithInt:0]; 
    Stock *akcie2 = [NSEntityDescription insertNewObjectForEntityForName:@"Stock" inManagedObjectContext:context]; 
    akcie2.ticker = @"HIMX"; 
    akcie2.index = [NSNumber numberWithInt:1]; 
    Stock *akcie3 = [NSEntityDescription insertNewObjectForEntityForName:@"Stock" inManagedObjectContext:context]; 
    akcie3.ticker = @"BRK-B"; 
    akcie3.index = [NSNumber numberWithInt:2]; 

    NSError *error; 
    if (![context save:&error]) { 
     NSLog(@"Error: %@", [error localizedDescription]); 
    } 
    [self aktualizaceDatTrhu]; // method to get data from web declared in Appdelegate aswell. 
} 

ViewController.h 
@property (nonatomic, strong) NSManagedObjectContext* managedObjectContext; 
@property (nonatomic, strong) NSArray *ulozeneAkcie; 

ViewController.m 
@interface ViewController() 
@end 

@implementation ViewController 
@synthesize managedObjectContext; 
@synthesize ulozeneAkcie; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSManagedObjectContext *context = managedObjectContext; 
    **NSLog(@"2");** 
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Stock" inManagedObjectContext:context]; 
    NSLog(@"3"); 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSLog(@"4"); 
    [request setEntity:entityDescription]; 
    NSLog(@"5"); 
    // Nastav trideni do arraye 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] 
             initWithKey:@"index" ascending:YES]; 
    [request setSortDescriptors:@[sortDescriptor]]; 
    NSLog(@"6"); 
    NSError *error; 
    ulozeneAkcie = [managedObjectContext executeFetchRequest:request error:&error]; 
    NSLog(@"7"); 
    } 

现在我认为问题在于viewcontroller无法访问managedobjectcontext。但我认为coredata将被视为全球性的,可以从应用程序的任何部分访问,如一盒乐高积木。

非常感谢任何人,只要在这里读到甚至更深的感谢任何提供见解的人。

如果有人觉得这样我就会喜欢拿一个放哪个属性的地方的例子,在哪里放置哪些综合和钩子,这样就可以实际工作。

PS:我发现了一些“相似”的问题,在网上和那些谈到传递MOC到ViewController,但因为我的应用程序是用故事板制作我会错过很多的钩子和网点和增加他们犯规解决任何问题..

+1

该错误消息指示'context'(第二个参数)是'nil'。 NSManagedObjectContext和其他核心数据组件不是“全局的”。通常,核心数据通过应用程序委托进行初始化/管理。建议阅读http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/CoreData/cdProgrammingGuide.html,并阅读核心数据教程,例如http://www.raywenderlich。COM/934 /核心数据上IOS-5-教程工具入门 – bobnoble 2013-03-24 11:09:52

回答

0

根据你的问题你应该检查两件不同的事情。

首先,您是否正确设置了核心数据堆栈?通常,像苹果一样,这可以在应用程序委托中设置。

然后,你正确地注入上下文引用?这可以如下执行。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"segueIdentifier"]) 
    { 
     ViewController *vc = [segue destinationViewController]; 
     vc.managedObjectContext = mainContext; // pass here the context that you created in app delegate or you grabbed from another property, for example 
    } 
} 

现在,在viewDidLoad

[super viewDidLoad]; 

if(!self.managedObjectContext) // see if context is nil 
    NSAssert(NO, @"The context cannot be nil"); // just for test purposes 

NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Stock" inManagedObjectContext:context]; 
NSLog(@"3"); 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSLog(@"4"); 
[request setEntity:entityDescription]; 
NSLog(@"5"); 
// Nastav trideni do arraye 
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] 
            initWithKey:@"index" ascending:YES]; 
[request setSortDescriptors:@[sortDescriptor]]; 
NSLog(@"6"); 
NSError *error; 
ulozeneAkcie = [managedObjectContext executeFetchRequest:request error:&error]; 
NSLog(@"7"); 
相关问题