我构建了一个通过WLAN发送OSC-Messages的应用程序 这就是为什么我有一个“网络”实体,其中包含一个对象。因此,我想要一个Singleton来获取这个对象 在AppDelegate中,我创建了一个Classmethod来获取ManagedObjectContext
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 APP在[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_不是他最擅长的 - 获取
它不是零,但我无法理解错误消息,并没有发现谷歌或其他任何地方
问题可能是,我需要自己初始化fetchedResultsController,因为它不会像生成的视图控制器那样自动初始化。
感谢您的帮助
答案 0 :(得分:3)
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext_ sectionNameKeyPath:nil cacheName:nil];
fetchedResultsController_ = aFetchedResultsController;
[aFetchedResultsController release];
在此代码块之后使用fetchedResultsController_
时,它已经被释放。因为你分配了它(retainCount + 1)然后你释放了它(retainCount-1)。当您将fetFetchedResultsController分配给fetchedResultsController _时,您没有保留。
要么像fetchedResultsController_ = [aFetchedResultsController retain];
那样保留它
或删除行[aFetchedResultsController release];
您应该使用fetchedResultsController_
替换fetchedResultsController
getter之外的所有self.fetchedResultsController
。通常,变量后面或前面的_应该告诉你应该使用setter和getter,除非你确定你在做什么。
fetchedResultsController_ = [NetworkSingleton fetchedResultsController];
调用fetchedResultsController_ = aFetchedResultsController;
这可能有用,但我不会这样做。