我構建了一個應用程序來通過WLAN發送OSC消息。
這就是爲什麼我有一個「網絡」實體與一個單一的對象。造成這種情況的原因是,我想要一個Singleton來獲取這個對象。
在AppDelegate中我創建了一個類方法來獲取ManagedObjectContextSingleton獲取CoreData,在[fetchedResultsController_ performFetch:&error]上崩潰
static NSManagedObjectContext* manObCon;
@implementation...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
manObCon = self.managedObjectContext;
...
}
+ (NSManagedObjectContext*) getManObCon{
return manObCon;
}
的managedObjectContext到來,在我單身的地址,所以我認爲它應該工作。
Singleton.h(這裏沒有什麼特別)
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "OSC_iPadAppDelegate.h";
#import "Network.h";
@interface NetworkSingleton : NSObject <NSFetchedResultsControllerDelegate> {
}
+ (Network*) getNetwork;
+ (void) insertNewObject;
+ (NSFetchedResultsController *)fetchedResultsController;
@end
Singleton.m應用程序崩潰AT [fetchedResultsController_ performFetch:&error]
與
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.2.1 (8C148)/Symbols/usr/lib/info/dns.so (file not found).
Program received signal: 「EXC_BAD_ACCESS」.
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.2.1 (8C148)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
kill
quit
繼承人Singleton.m準則
#import "NetworkSingleton.h"
static Network* _network;
static NSFetchedResultsController *fetchedResultsController_;
static NSManagedObjectContext *managedObjectContext_;
@implementation NetworkSingleton
+ (Network*) getNetwork{
managedObjectContext_ = [OSC_iPadAppDelegate getManObCon]; //get the managedObjectContex from AppDelegate
//fetchedResultsController init
fetchedResultsController_ = [NetworkSingleton fetchedResultsController]; //get fetchedResultsController
//check if _network is set
if (_network == nil) {
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController_ sections] objectAtIndex:0];
//if not set, check if there is already an network object in coredata
if ([sectionInfo numberOfObjects] == 0) {
//Create new Networkobject, if no one is existing
[NetworkSingleton insertNewObject];
fetchedResultsController_ = nil;
}
//set _network
_network = [fetchedResultsController_ objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}
return _network;
}
+ (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController_ != nil) {
return fetchedResultsController_;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Network" inManagedObjectContext: managedObjectContext_];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:1];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sourcePort" ascending:YES selector:nil];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext_ sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
fetchedResultsController_ = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
NSError *error = nil;
/*
APP CRASHES HER
*/
if (![fetchedResultsController_ performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return fetchedResultsController_;
}
@end
所以我不知道爲什麼fetchedResultsController_不是他最擅長的 - 取得
它不是零,但我不能瞭解ErrorMessage,並且找不到Google或其他地方的任何東西。
問題可能是,我需要自己初始化fetchedResultsController,因爲它不會像生成的視圖控制器中那樣自動初始化。
感謝您的幫助
這是一個糟糕的設計。 fetchedResultsController旨在成爲核心數據模型層和UITableView視圖層之間的控制器。它擊敗了FRC的目的是讓它由一個單例模型對象持有。你已經粉碎了你的模型和控制層,並將你的tableview硬編碼爲數據。 – TechZen 2011-03-11 16:51:23
在這種情況下,我不使用它作爲TableView。我需要單身人士,因爲我在許多不是意見的班級使用數據。 但是我如何保存沒有CoreData的單個實例? – Seega 2011-03-15 08:24:10