2013-03-03 48 views
0

我有一個使用CoreData數據庫的TableViewController。 我有另一個UIviewController我想從中讀取TableViewController的數據庫。 我所做的是如下。從TableViewController的CoreData數據庫提取數據

//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];} 

我有

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext; 

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

一個屬性,因此,當它達到該行的程序總是崩潰。 可能是什麼問題?

+0

你在哪裏創建NSManagedObjectContext? – ColinE 2013-03-03 07:10:01

+0

在ViewDidLoad中作爲_managedObjectContext = [[NSManagedObjectContext alloc] init]; – Bryanyan 2013-03-03 07:45:52

+0

事情是如果我把_managedObjectContext = [[NSManagedObjectContext alloc] init];在ViewDidLoad中,_managedObjectContext不是零。但是當程序到達行NSEntityDescription * entity = [NSEntityDescription entityForName:@「TrackerList」 inManagedObjectContext:self.managedObjectContext]時仍然崩潰; – Bryanyan 2013-03-03 07:49:15

回答

0

通常,您可以使用在AppDelegate的存根代碼中提供給您的managedObjectContext。如果多數民衆贊成的情況下,你可以使用:

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

然後以替代線路:

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

使用:

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

此時應更換return語句這樣:

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

如果你正在創建你的o那麼你應該設置其persistentStoreCoordinator(它依次需要託管對象模型並設置持久性存儲類型)。

如果在創建項目時勾選了'use core-data',您可以看到如何在AppDelegate.m中完成所有操作。

無論如何,你已經在你的第一個視圖控制器中成功地使用了managedObjectContext。所以你只需要在第二個視圖控制器中獲取相同的對象。

對於你的方法工作,你只需要在您提供的代碼塊的頂部添加一行:

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

我已經更新了答案,以顯示您爲該方法工作所做的工作,因爲您使用的是AppDelegate提供的managedObjectContext。 – Rakesh 2013-03-03 13:05:20

+0

謝謝,你是最好的方法。這樣我就可以訪問現有的數據庫。現在我使用你的方法,它的工作原理。但對於MasterViewController,它有它自己的NSManagedObjectContext,並且無法找到它如何鏈接到AppDelegate的NSManagedObjectContext.Anyway謝謝。 – Bryanyan 2013-03-03 13:12:01

+0

太好了。如果你認爲這是解決方案,你可以接受我的答案/如果你不介意的話,可以給它一個upvote。 :) – Rakesh 2013-03-03 13:13:24

0

管理對象上下文需要與持久性存儲協調員初始化並且需要管理對象模型。 XCode用於爲AppDelegate中的所有這些實現提供一個鍋爐板代碼。

作爲一種替代解決方案,您可以嘗試使用MagicalRecord

您可以通過

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

建立核心數據,並可以通過

NSManagedObjectContext *context = [NSManagedObjectContext defaultContext]; 
[TrackerList findAllSortedBy:@"descript" ascending:YES inContext:context]; 
獲取在上下文中的所有trackerlist值

關注鏈接將指引您獲得更佳效果 How to make programming in Core Data pleasant

+0

是的,在AppDelegate中,所有與CoreData相關的對象都被初始化。但在MasterViewController中,它只有 - @屬性(強,非原子)NSFetchedResultsController * fetchedResultsController; - @屬性(強,非原子)NSManagedObjectContext * managedObjectContext;我甚至沒有看到managedObjectContext的init。我很困惑它是如何在MasterViewController中實現的。 – Bryanyan 2013-03-03 12:49:25

+0

@Bryanyan:你是如何在MasterViewController中獲得你的managedObjectContext的?它是第一個視圖控制器還是第二個? – Rakesh 2013-03-03 12:59:38

+0

當我爲新項目選擇Master/Detail Application時,managedObjectContext已由XCode在第一個視圖控制器中創建。 – Bryanyan 2013-03-03 13:24:27