Tidane,
당신이 개체가 이미 존재 여부, 당신이 가져 오기 요청을 설정하고 특정 조건을 사용해야 할 경우 확인해야
. 귀하의 경우에는
Ville
에 대해 간단한 가져 오기를 수행하고
idV
이 이미 있는지 확인할 수 있습니다. 나는
idV
이 귀하의
Ville
에 대한 식별자와 같다고 생각합니다.
nom
을 확인하는 것은 같을 수 있지만 세계의 두 곳에서 같은 이름을 가진 두 개의
cities
을 가질 수 있습니다.
@alpz 해결책은 정상입니다. 또한 당신은 또한 1로 fecth 제한을 설정할 수 있습니다 : 그렇지 않으면
[request setFetchLimit:1];
를 사용하면 간단하게
NSUInteger count = [context countForFetchRequest:request error:&error];
if(count == 0) {
// add the new city here
}
대신
NSArray *results = [context executeFetchRequest:request error:nil];
@Phillip 밀스의 당신이 할 수 있다고 말하고있다 할 수 키 값 코딩 액세스 대신 NSManagedObject
하위 클래스를 사용하십시오. 일반 문자열 키 대신 클래스의 속성을 사용하는 것이 더 쉽다고 생각합니다. 당신이 Ville
서브 클래스 (organising-core-data-for-ios)를 만든 후에
그래서, 당신은 단지 Ville+Extension
라고 그것을 위해 카테고리를 만들 수 있고, 다음과 같은 방법을 만들 : 다음
+ (Ville *)villeWithUniqueName:(NSString *)name withManagedObjectContext:(NSManagedObjectContext*)context
{
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Ville" inManagedObjectContext:context]];
[request setPredicate:[NSPredicate predicateWithFormat:@"nom = %@", name];
[request setFetchLimit:1];
NSArray *results = [context executeFetchRequest:request error:nil];
Ville* theVille = nil;
if ([results count] == 0)
{
theVille = // create the new here
}
else
{
theVille = (Ville*)[results objectAtIndex:0];
}
[request release]; // if you don't use ARC
return theVille;
}
, (해당 범주를 사용 수입 후) :
Ville* theVille = [Ville villeWithUniqueName:@"yourName" withManagedObjectContext:yourContext];
희망이 있습니다.
대단히 고마워 ... 마침내 생각이났다. :) – Tidane
@Tedane 오신 것을 환영합니다.건배. –