나는 coreData에 저장하고 iOS dev을 사용하는 것에 대해 새로운 경험이있다.coredata에 일대 다 관계를 적절하게 저장하는 방법
내가 고유 식별자/idFB
로 끌어왔다 내 DB에서 사용자를 가질 수 있도록하려면 해당 사용자 생성 및 루틴을 자신의 작업을 검색 할 수 있습니다 내가 달성하기 위해 노력하고 무엇
.
어디까지 갔습니까?
내가 제대로 잘 User
와 연결된 Routine entity
에서 routineName
을 retriev 방법을 만들기 위해 (내 생각) 관리. fetch
방법을 참조하십시오.
내 문제 :
내가 올바른 엔티티 관계 협회 User (usersExercise) <--->> Routine (userID)
에 저장하고 있지 않다 생각합니다. 순서대로 나는 내 save
방법이 옳지 않다라고 생각한다. 나는 전체 사용자를 userID로 저장하고 있고, 그 느낌이 옳지 않느냐? 주로 Routine.userID
이 튀어 나오면 특정 ID 대신 관련 사용자 전체를 끌어 당깁니다. 나는 정말로 무엇을 기대 해야할지 모른다.
아무도 내가이 방법을 올바르게 만들 것을 도울 수 없습니까? 나는 coreData의 전체 과정을 저장하고 올바른 관계를 유지하는 데 매우 혼란 스럽다. 내가, [(사용자 *)이 일치 ("현재 결과 %에 @를 얻을"@ 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;
})
:
- (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];
}
이
페치에서 NSLog 인쇄하는 것입니다 [0] 사용자 루틴]); getCurrentUser 방법으로 I는 전체 사용자의 데이터를 얻을 수와의 관계는 당신이에 관계를 원하는 곳
usersExercise = "<relationship fault: 0xa464730 'usersExercise'>";
다른 루프에서이 줄 : (사용자 *)는 일치 반환; 리턴해야하는지 (User *)는 [0]과 일치합니다; – user523234