gpt4 book ai didi

objective-c - 单例获取核心数据,在 [fetchedResultsController performFetch :&error] 上崩溃

转载 作者:行者123 更新时间:2023-11-28 20:46:51 26 4
gpt4 key购买 nike

我构建了一个应用程序以通过 WLAN 发送 OSC 消息。
这就是为什么我有一个包含单个对象的“网络”实体。为此,我想要一个 Singleton 来获取这个对象。
在 AppDelegate 中,我创建了一个类方法来获取 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
它不是零,但我无法理解错误消息,并且在 Google 或其他地方找不到任何东西。
问题可能是,我需要自己初始化 fetchedResultsController,因为它不像在生成的 View Controller 中那样自动初始化。

感谢您的帮助

最佳答案

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 getter 之外的所有 fetchedResultsController_ 替换为 self.fetchedResultsController。通常,变量后面或前面的 _ 应该告诉您应该使用 setter 和 getter,除非您确定自己在做什么。

并且 fetchedResultsController_ = [NetworkSingleton fetchedResultsController]; 调用 fetchedResultsController_ = aFetchedResultsController;
这可能有用,但我不会这样做。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com