我建立了一个通过WLAN发送OSC消息的应用程序。
那就是为什么我有一个带有单个对象的“网络”实体。由于这个原因,我希望一个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应用程序在[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_不是他最擅长的-fetch
它不是零,但我听不懂Errormessage,却在Google或其他地方一无所获。
问题可能是,我需要自己初始化fetchedResultsController,因为它不会像生成的视图控制器那样自动初始化。

谢谢你的帮助

最佳答案

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext_ sectionNameKeyPath:nil cacheName:nil];

fetchedResultsController_ = aFetchedResultsController;

[aFetchedResultsController release];


在此代码块之后使用fetchedResultsController_时,它已被释放。因为您分配了它(retainCount + 1),然后释放了它(retainCount-1)。将fetchedResultsController分配给fetchedResultsController_时,您没有保留。

要么像这样fetchedResultsController_ = [aFetchedResultsController retain];保留它
或删除行[aFetchedResultsController release];



编辑:我只是看到您的代码中可能还有更多错误。

您应将fetchedResultsController_吸气剂之外的所有fetchedResultsController替换为self.fetchedResultsController。通常,除非您确定自己在做什么,否则变量后面或前面的_应该告诉您应该使用setter和getter。

并且fetchedResultsController_ = [NetworkSingleton fetchedResultsController];调用fetchedResultsController_ = aFetchedResultsController;
哪个可能有效,但我不会。

关于objective-c - Singleton获取核心数据,在[fetchedResultsController performFetch:&error]上崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5274824/

10-14 10:37