简体   繁体   English

从TableViewController的CoreData数据库中获取数据

[英]Fetching data from TableViewController's CoreData database

I have a TableViewController using the CoreData database. 我有一个使用CoreData数据库的TableViewController。 I have another UIviewController from which I want to read the TableViewController's database. 我有另一个UIviewController,我想从中读取TableViewController的数据库。 What I did was as follow. 我所做的如下。

//In UIviewController
-(NSArray *)fetchRecordedDatainsqldatabase
{
    // construct a fetch request

    NSError *error;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"TrackerList" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    //[fetchRequest setFetchBatchSize:20];
    // Create the sort descriptors array.
    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"descript" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:descriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    // return the result of executing the fetch request
    return [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];}

I have a property of 我有一个财产

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

But managedObjectContext is always nil, at the line
NSEntityDescription *entity = [NSEntityDescription entityForName:@"TrackerList" 
inManagedObjectContext:self.managedObjectContext];

So the program is always crash when it reaches to that line. 因此,程序到达该行时总是崩溃。 What could be the problem? 可能是什么问题呢?

Generally you can use the managedObjectContext provided to you in the stub code in AppDelegate . 通常,您可以在AppDelegate的存根代码中使用提供给您的managedObjectContext If thats the case you can use: 如果是这样,您可以使用:

AppDelegate *appD = [[UIApplication sharedApplication] delegate];

then instead of the line: 然后代替一行:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"TrackerList" 
inManagedObjectContext:self.managedObjectContext];

use: 采用:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"TrackerList" 
inManagedObjectContext:appD.managedObjectContext];

and you should replace the return statement to this: 并且您应该将return语句替换为:

return [appD.managedObjectContext executeFetchRequest:fetchRequest error:&error];

If you are creating your own NSManagedObjectContext object, then you should be setting its persistentStoreCoordinator (which in turns need the managed object model and set the persistent store type). 如果要创建自己的NSManagedObjectContext对象,则应设置其persistentStoreCoordinator (这反过来需要托管对象模型并设置持久性存储类型)。

You can see how to do all this in AppDelegate.m , if you have checked 'use core-data' when creating the project. 如果在创建项目时选中了“使用核心数据”,则可以在AppDelegate.m查看所有操作方法。

Anyway in your case, you already are using a managedObjectContext successfully in your first view controller. 无论如何,您已经在第一个视图控制器中成功使用了ManagedObjectContext。 So you just need to get the same object over in the second view controller. 因此,您只需要在第二个视图控制器中获取相同的对象即可。

For your method to work, you just have to add one line at the top of the code block you provided: 为了使方法起作用,您只需在提供的代码块的顶部添加一行:

self.managedObjectContext = [[[UIApplication sharedApplication] delegate] managedObjectContext];

Managed Object Context needs to be initialized with a Persistent Store Coordinator and which needs a Managed Object Model . 需要使用持久性存储协调器初始化托管对象上下文 ,并且托管托管上下文需要托管对象模型 XCode used to provide a boiler plate code for all these implementation in AppDelegate. XCode用于为AppDelegate中的所有这些实现提供样板代码。

As an alternative solution you can try using MagicalRecord 作为替代解决方案,您可以尝试使用MagicalRecord

You can set up core data by 您可以通过以下方式设置核心数据

[MagicalRecord setupCoreDataStackWithStoreNamed:@"Database.sqlite"];

And you can fetch all the trackerlist values in a context by 您可以通过以下方式获取上下文中的所有trackerlist值:

NSManagedObjectContext *context = [NSManagedObjectContext defaultContext];
[TrackerList findAllSortedBy:@"descript" ascending:YES inContext:context];

Following link would guide you better How to make programming in Core Data pleasant 以下链接将更好地指导您如何使Core Data中的编程愉快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM