iOS EXC_BAD_ACCESS代码= 1核心数据

时间:2014-08-13 12:11:16

标签: ios

我的核心数据存在问题。在视图控制器方法viewDidLoad中,我请求:

NSLog(@"%@", [databaseManagementService selectUsers]);

请求后我收到错误:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x80945a35)

任何人都可以帮我处理这个问题吗?我的代码:

- (NSArray *)selectUsers
{
    NSMutableArray *userObjects = [[NSMutableArray alloc] init];
    NSArray *userManagedObjects = [self queryUsers];

    if (userManagedObjects)
    {
        for (int i = 0; i < userManagedObjects.count; i++)
        {
            TaskManagerUser *userObject = [[TaskManagerUser alloc] init];
            User *userManagedObject = userManagedObjects[i];

            userObject.id = userManagedObject.id;
            userObject.name = userManagedObject.name;
            userObject.surname = userManagedObject.surname;

            userObject.counterValue = userManagedObject.counterValue;
            userObject.gpsLog = userManagedObject.gpsLog;
            userObject.material = userManagedObject.material;
            userObject.taskNew = userManagedObject.newTask;
            userObject.taskAssignNew = userManagedObject.newTaskAssign;
            userObject.taskPictureNew = userManagedObject.newTaskPicture;
            userObject.taskAction = userManagedObject.taskAction;
            userObject.taskPicture = userManagedObject.taskPicture;

            [userObjects addObject:userObject];
        }
    }

    else
    {
        return nil;
    }

    return userObjects;
}

我试过调试,在返回行中userObjects看起来很好......有人可以帮我吗?

queryUsers:

- (NSArray *)queryUsers
{
    NSArray *usersManagedObjectsList = [self queryEntitiesForName:entityUser withPredicate:nil];
    if (usersManagedObjectsList != nil && usersManagedObjectsList.count > 0)
    {
        return usersManagedObjectsList;
    }
    return nil;
}

也:

- (NSArray *)queryEntitiesForName:(NSString *)name withPredicate:(NSPredicate *)predicate
{
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:name inManagedObjectContext:self.managedObjectContext];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];
    [request setPredicate:predicate];
    [request setIncludesSubentities:YES];

    NSError *error = nil;
    NSArray *array = [self.managedObjectContext executeFetchRequest:request error:&error];
    if (array == nil)
    {
        TaskManagerAlertErrorService *taskManagerAlertErrorService = [[TaskManagerAlertErrorService alloc] init];

        [taskManagerAlertErrorService alertErrorWithTitle:JSONDatabaseManagementServiceErrorTitle andMessage:JSONDatabaseManagementServiceErrorMessage andCancelButtonTitle:JSONDatabaseManagementServiceErrorCancelButtonTitle];
    }
    return array;
}

此外,控制台中没有堆栈跟踪...

3 个答案:

答案 0 :(得分:1)

这是否有效:

userObjects[i] = userObject;

数组如何知道分配足够的内存来分配索引“i”的对象?

我建议使用:

[userObjects addObject:userObject];

答案 1 :(得分:0)

由于您指定使用CoreData时遇到问题,我猜TaskUserManager是 NSManagedObject 的子类?

如果是,那么您的错误就在这里:

TaskManagerUser *userObject = [[TaskManagerUser alloc] init];

您无法使用init分配CoreData实体。您需要使用对象上下文并使用以下行:

[objectContext insertNewObjectForEntityForName:@"TaskUserManager"]

答案 2 :(得分:-1)

最后我找到了答案!错误在这里:

userObject.taskNew = userManagedObject.newTask;
userObject.taskAssignNew = userManagedObject.newTaskAssign;
userObject.taskPictureNew = userManagedObject.newTaskPicture;

托管对象属性不能以&#34; new&#34;开头。

userObject.taskNew = userManagedObject.taskNew;
userObject.taskAssignNew = userManagedObject.taskAssignNew;
userObject.taskPictureNew = userManagedObject.taskPictureNew;

完美无缺!