gpt4 book ai didi

ios - 数据 : issue in another function after fetching data from core data

转载 作者:行者123 更新时间:2023-11-29 02:02:10 25 4
gpt4 key购买 nike

我是 coredata 的新手,几天前我开始使用 core data。如果我以当前方法记录数据,我已经编写了一个谓词来从 coredata 中获取数据,它工作正常。如果我用另一种方法记录数据,它会显示“数据:”

<Profile: 0x16451ba0> (entity: Profile; id: 0x1633c150 <x-coredata://41971DAD-4658-4C38-9D14-7FDFFA57E032/Profile/p6> ; data: <fault>)

-(void)populateCurrentUserData{

self.blockListArray = [self dataForJid:[[DataManager sharedHandler]userToken]];
Profile *profile = [self.blockListArray objectAtIndex:0];
NSLog(@"Data is :%@",profile.userId);//prints nil
NSLog(@"Data is :%@",self.blockListArray); //"<Profile: 0x17bb23f0> (entity: Profile; id: 0x17bb1fe0 <x-coredata://41971DAD-4658-4C38-9D14-7FDFFA57E032/Profile/p1> ; data: <fault>)"

}

-(NSArray *)dataForJid:(NSString *)inJid{
NSArray *data = [[NSArray alloc]init];
NSError *error;
self.dataArray = [[NSMutableArray alloc]init];
MKAUserProfileCoreData *storage = [[MKAUserProfileCoreData alloc]init];
NSManagedObjectContext *moc = [storage managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Profile" inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSPredicate *profilePredicate = [NSPredicate predicateWithFormat:@"userId = %@", inJid];
[request setPredicate:profilePredicate];
[request setEntity:entity];
[request setReturnsObjectsAsFaults:NO];
data = [moc executeFetchRequest:request error:&error];
NSLog(@"Data is :%@",data); //This log works fine

return data;

/.h
#import <Foundation/Foundation.h>

@interface MKAUserProfileCoreData : NSObject
{
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
}
@property (nonatomic, strong, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, strong, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@end

/.m


#import "MKAUserProfileCoreData.h"
#import <CoreData/CoreData.h>

@implementation MKAUserProfileCoreData
- (NSManagedObjectContext *) managedObjectContext {

if (managedObjectContext != nil) {
return managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext;

   - (NSManagedObjectModel *)managedObjectModel {

if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return managedObjectModel;
}





- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}

NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"splashUserProfile.sqlite"]];

NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:storeUrl options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error:&error]) {
NSLog(@"Error is %@",error);
/*
Replace this implementation with code to handle the error appropriately.

abort() causes the application to generate a crash log and terminate. You should
not use this function in a shipping application, although it may be useful during
development. If it is not possible to recover from the error, display an alert panel that
instructs the user to quit the application by pressing the Home button.

Typical reasons for an error here include:
* The persistent store is not accessible
* The schema for the persistent store is incompatible with current managed object
model
Check the error message to determine what the actual problem was.
*/
abort();
}

return persistentStoreCoordinator;
}

- (NSString *)applicationDocumentsDirectory {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//NSLog(@"Path is %@", path);
return path;
}

@end

最佳答案

问题是 storagemoc 都是在 dataForJid: 中本地创建的。当该方法完成时,这两个变量都将被释放。因此 NSManagedObjects 变得无效。您需要保持对 CoreData 堆栈的强引用 - 例如,将 MKAUserProfileCoreData 设为单例,或者直接在 View Controller 中构建堆栈。

关于ios - 数据 : <fault> issue in another function after fetching data from core data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30216251/

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