我在我的應用程序中使用CoreData,它是一對多的關係,我有兩個實體folders
和files
。所以一個文件夾可以有很多文件。因此,我的第一個視圖控制器顯示所有文件夾,並在每個文件夾上單擊時顯示其中的文件。在CoreData中傳遞對象
現在其中的所有文件夾和文件都是由用戶動態創建的,並非來自任何數據庫或來自Web服務器。
現在我有一個更多的視圖控制器,我從應用程序委託打開,在這裏我想顯示一些文件。
我知道,首先要通過managedObjectContext
,這將在應用程序委託聲明
ViewController *View = [[ViewController alloc]initWithNibName: @"ViewController" bundle:nil]
View.managedObjectContext = self.managedObjectContext;
因此,如果我們只是傳遞managedObjectContext
,我們可以訪問該文件的實體對象,或者我們通過文件實體對象也。
現在你知道,我有兩個實體,名爲Folder
和File
,現在可以訪問存儲在任何文件夾中的文件,我只是這樣做。
NSMutableArray *filesArray = [self.folder.file mutableCopy];
但現在,我想表明我想在Anyview的文件,它可以二三導航後打開,或者直接從appDelegate.m打開。
要非常清楚,我的問題是如何做到這一點?即如何從AppDelegate
獲得filesArray。
根據以下回答編輯。我創建了一個fetchResultsController這樣
- (NSFetchedResultsController *)fetchedResultsController {
if (m_fetchResultsController != nil) {
return m_fetchResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"File" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"alarm" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"alarm!= nil"]];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return m_fetchResultsController;
}
和viewDidLoad中,我這樣寫
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
NSMutableArray *alarms = [[self.fetchResultsController fetchedObjects]mutableCopy];
NSLog(@"alarmsCount:%d",[alarms count]);
}
問候 蘭吉特。
編輯您的問題,並寫下您的疑問;) –
檢查此問題,http://stackoverflow.com/questions/14170772/move-rows-to-bottom-in-a-tableview-which-uses-coredata – Ranjit
嗨,現在我有點忙。我會盡快閱讀你的問題。 –