3

我对保存到 coreData 和使用 iOS 开发者很陌生。

我想要达到的目标:

我希望能够在我的数据库中拥有一个具有唯一标识符 / 的用户,idFB并且该用户可以创建和检索他们的锻炼例程。

我走了多远?

我设法(我认为)创建了一种方法,可以routineNameRoutine entity与 right 关联的中正确检索User。见fetch方法。

我的问题:

我想我没有保存正确的实体关系关联User (usersExercise) <--->> Routine (userID)。换句话说,我认为我的save方法不正确......因为我将整个用户保存到 userID 并且感觉不对?主要是因为当它吐出时,Routine.userID它会拉出整个关联用户而不是特定的 ID?我真的不知道会发生什么

谁能帮我正确构建这些方法?我对 coreData 保存和建立正确关系的整个过程感到非常困惑。

在此处输入图像描述

- (void) save {
    Routine *newRoutine = [NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:context];

    newRoutine.users = [self getCurrentUser];
    newRoutine.routineName = @"myRoutine Test Name";
    NSError* error;
    [context save:&error ];

    NSLog(@"Saved now try to fetch");
    [self fetch];

}
-(void) fetch {
    NSFetchRequest *fetchRequestItems = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityItem = [NSEntityDescription entityForName:@"Routine" inManagedObjectContext:context];
    [fetchRequestItems setEntity:entityItem];
    User* user = [self getCurrentUser];
    // if i try [[self getCurrentUser] usersRoutine] it shows an error

    [fetchRequestItems setPredicate:[NSPredicate predicateWithFormat:@"users == %@",user]];

    //Sort by last edit ordered
    NSArray *sortDescriptors = [NSArray arrayWithObjects:nil];
    [fetchRequestItems setSortDescriptors:sortDescriptors];
    NSError *error = nil;
    NSArray* Routines = [context executeFetchRequest:fetchRequestItems error:&error];
    NSLog(@"result %@", [(Routine *)Routines[0] users]  );


}
-(User *)getCurrentUser {
    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];

    if (_appDelegate.isFB)
    { 
        request.predicate = [NSPredicate predicateWithFormat:@"idFB LIKE %@",_appDelegate.fdID];
        NSError *error = nil;
        NSArray *matches = [[context executeFetchRequest:request error:&error] mutableCopy];

        return (User *)matches[0];
    } else
    {
        NSLog(@"CreateRoutinePOPUP NON FB TO BE TESTED");
        request.predicate = [NSPredicate predicateWithFormat:@"email LIKE %@",_appDelegate.currentUser];
        NSError *error = nil;
        NSArray *matches = [[context executeFetchRequest:request error:&error] mutableCopy];

        return (User *)matches[0];
    }

这是 fetch 中的 NSLog 正在打印的内容:

2013-04-28 22:33:26.555 iGym[7916:c07] result <User: 0xa480580> (entity: User; id: 0xa495a00 <x-coredata://D87CEBB4-016C-4A1B-802C-2D1117BB3E51/User/p1> ; data: {
    dob = "1986-12-26 00:00:00 +0000";
    email = ".com";
    firstTime = nil;
    gender = male;
    height = nil;
    idFB =3333;
    idUserExternal = 0;
    idUserInternal = 0;
    isPT = nil;
    language = "en_US";
    location = "London, United Kingdom";
    metricSystem = nil;
    name = Joan;
    nickname = nil;
    password = nil;
    surname = Thurft;
    usersExercise = "<relationship fault: 0xa4824a0 'usersExercise'>";
    usersRoutine =     (
        "0xa495f00 <x-coredata://D87CEBB4-016C-4A1B-802C-2D1117BB3E51/Routine/p6>",
        "0xa4877e0 <x-coredata://D87CEBB4-016C-4A1B-802C-2D1117BB3E51/Routine/p1>",
        "0xa4877f0 <x-coredata://D87CEBB4-016C-4A1B-802C-2D1117BB3E51/Routine/p2>",
        "0xa487800 <x-coredata://D87CEBB4-016C-4A1B-802C-2D1117BB3E51/Routine/p3>",
        "0xa487810 <x-coredata://D87CEBB4-016C-4A1B-802C-2D1117BB3E51/Routine/p4>",
        "0xa487820 <x-coredata://D87CEBB4-016C-4A1B-802C-2D1117BB3E51/Routine/p5>"
    );
    weight = nil;
})

当我添加 NSLog(@"get current result %@", [(User *)matches[0] usersRoutine]); 到 getCurrentUser 方法,我得到了整个用户的数据,关系说

usersExercise = "<relationship fault: 0xa464730 'usersExercise'>";
4

2 回答 2

7

Core Data 并不完全像使用标准数据库一样,您将一些外键分配userID给另一个您想要与User对象建立关系的表,然后使用该外部 ID 来查找关系,例如exercise.where('user_id = ?', userID). 相反,您定义实际关系并让 Core Data 处理幕后的所有事情,以设置任何连接表或外键。

而不是你如何设置它,你只需要在User实体中有两个关系exercisesroutines它们被映射到ExerciseandRoutine实体,然后你会在Exerciseand上有Routine一个反向关系,users如果它是一个 has-and-belongs-多对多关系。所以现在,您需要将and实体替换usersExerciseexercises, usersRoutinewithroutinesuserIDwith 。usersExerciseRoutine

即使您实际上并不需要这种反向关系,您仍然需要它,因为 Core Data 将它用于数据完整性目的,如果您不填充它,Xcode 会给您一个警告。

当您建立这些关系时,您将调用例程或练习,如user.exercises返回该用户的相关练习集。正如您所注意到的,Core Data 将返回他们所谓的 afault来表示将被触发的关系,并在您实际需要该关系的内容时返回数据。存在错误,因此您只返回您需要的确切信息,而不是对数据集运行不必要的查询。

需要注意的另一件事是,Core Data 不会像userID您所做的那样引用唯一 ID。相反,Core Data 中的每个对象都有一个唯一的 ID [objectName objectID](只有在保存到数据存储后才是永久的)。除了特殊情况外,您确实不需要将唯一 ID 设置为实体的属性。

此外,您真的不需要使用那些 unique objectID,除非您像在多线程应用程序中那样传递对象以进行后台处理,在这种情况下NSManagedObjectID是线程安全的,您可以使用它在 a 上再次查找对象后台线程/托管对象上下文。

我真的建议阅读 Core Data 的不错介绍,例如http://www.raywenderlich.com/934/core-data-on-ios-5-tutorial-getting-started

如果您习惯于正常的数据库设置/架构,在第一次转换到 Core Data 时可能会有些奇怪,但是一旦您习惯了它,它实际上会快很多,并且可以为您处理幕后的所有艰苦工作。


从评论更新:

您误解了 Core Data 中关系的概念。在 Core Data 中,关系不会像典型的数据库连接关系那样返回关联的 ID。相反,它会返回一个错误,当您需要该关系中的数据时会触发该错误。所以它不是返回整个User对象,而是返回fault关联的User对象,当你做类似的事情时它会被触发和查询exercise.user.name

您的代码的工作方式与您保存时应该完全一样,您只是错误地假设它不是。

于 2013-04-28T22:07:12.973 回答
0

您需要使用提供的方法在一对多对象中添加“多对象”。在你的情况下,它被称为addRoutineObject:

试试这个新的保存方法:

- (void) save {

    Routine *newRoutine = [NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:context];
    newRoutine.routineName = @"myRoutine Test Name";

    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];


    NSArray *matches;
    NSError *error = nil;
    if (_appDelegate.isFB)
    { 
        request.predicate = [NSPredicate predicateWithFormat:@"idFB LIKE %@",_appDelegate.fdID];
        matches = [[context executeFetchRequest:request error:&error] mutableCopy];

    } else
    {
        NSLog(@"CreateRoutinePOPUP NON FB TO BE TESTED");
        request.predicate = [NSPredicate predicateWithFormat:@"email LIKE %@",_appDelegate.currentUser];
        matches = [[context executeFetchRequest:request error:&error] mutableCopy];
    }

    if (matches.count == 0)
    {
        NSLog(@"no user matched");
    }
    else
    {
        User *aUser = [matches objectAtIndex:0];
        [aUser addRoutineObject:newRoutine];
        if (![context save:&error])
        {
            NSLog(@"couldn't save: %@", [error localizedDescription]);
        }
    }
   }
于 2013-04-28T23:07:28.690 回答