开发者

Singleton to fetch CoreData, crashes on [fetchedResultsController_ performFetch:&error]

开发者 https://www.devze.com 2023-02-16 10:54 出处:网络
i build an app to send OSC-Messages through WLAN. Thats why i have a \"Network\" entity with a single object in it. Cause of this, i want a Singleton to fetch this object.

i build an app to send OSC-Messages through WLAN.

Thats why i have a "Network" entity with a single object in it. Cause of this, i want a Singleton to fetch this object.

In AppDelegate i created a Classmethod to get the ManagedObjectContext

static NSManagedObjectContext* manObCon;

@implementation...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
   manObCon = self.managedObjectContext;
...
}

+ (NSManagedObjectContext*) getManObCon{
   return manObCon;
}

the managedObjectContext arrives with an address in my singleton, so i think it should work.

Singleton.h (nothing special here)

#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 CRASHES AT [fetchedResultsController_ performFetch:&error] with

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

Heres the Code of 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

So i dont know why the fetchedResultsController_ doesn't what he does best - fetch

it isn't nil but i can't understand the Errormessage and found nothing ob Google or somewhere else.

The problem could be, that i need to init the fetchedResultsController by myself, because it doesn't init automatically like in generated view controllers.

Thanks for your Help


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

fetchedResultsController_ = aFetchedResultsController;

[aFetchedResultsController release];

When you use fetchedResultsController_ after this block of code it is released already. Because you allocated it (retainCount+1) and then you released it (retainCount-1). You didn't retain when you assigned aFetchedResultsController to fetchedResultsController_.

either retain it like this fetchedResultsController_ = [aFetchedResultsController retain];
or remove the line [aFetchedResultsController release];


EDIT: I just saw that there might be more wrong in your code.

You should replace all fetchedResultsController_ outside of the fetchedResultsController getter with self.fetchedResultsController. Normally the _ behind or in front of a variable should tell you that you should use the setter and getter unless you are sure what you are doing.

And fetchedResultsController_ = [NetworkSingleton fetchedResultsController]; invokes fetchedResultsController_ = aFetchedResultsController;.
Which might work, but I wouldn't do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消