2013-10-16 16 views
9

Sto provando a recuperare un singolo oggetto dal mio database principale, tuttavia continua a restituire null. Il mio metodo si basa su un altro metodo che restituisce ogni valore dall'oggetto coredata a cui sto accedendo.Come recuperare un singolo oggetto da Coredata usando un predicato

Non l'ho mai provato prima e ho provato a leggere i documenti di mele ma non ha proprio senso ... questo è il mio metodo sembra

- (NSMutableArray *)readSelectedInstall:(NSString *)projIDString { 
    NSManagedObjectContext *context = [self managedObjectContext]; 

    if (context == nil) { 
     NSLog(@"Nil"); 
    } 
    else { 


     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
     NSEntityDescription *entity = [NSEntityDescription entityForName:@"InstallProject" inManagedObjectContext:context]; 
     [fetchRequest setEntity:entity]; 

     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ProjID==%@", projIDString]; 
     [fetchRequest setPredicate:predicate]; 

     NSError *error; 

     NSMutableArray *installProjectDictionaryArray = [[NSMutableArray alloc] init]; 


     NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 
     for (InstallProject *installProj in fetchedObjects) { 

      NSMutableDictionary *tempInstallProjectDictionaryArray = [[ NSMutableDictionary alloc] init]; 

      [tempInstallProjectDictionaryArray setObject:installProj.companyName forKey:@"CompanyName"]; 
      [tempInstallProjectDictionaryArray setObject:installProj.projNo forKey:@"ProjNo"]; 
      [tempInstallProjectDictionaryArray setObject:installProj.projID forKey:@"ProjID"]; 

      [installProjectDictionaryArray addObject:tempInstallProjectDictionaryArray]; 
     } 

     return installProjectDictionaryArray; 
    } 
    return nil; 
} 

alcun aiuto me sempre per restituire un singolo elemento questo è projID corrisponde al projIDString sarebbe molto apprezzato.

+0

è 'contesto == quelle negative ? Oppure 'executeFetchRequest' restituisce' nil'? In quest'ultimo caso, qual è il valore di 'error'? –

+0

risulta che non stavo assegnando la mia classe CoredataManagins .. L'ho fatto ora e sto ricevendo un errore. "Terminazione dell'app a causa dell'eccezione non rilevata 'NSInvalidArgumentException', motivo: 'generazione SQL non implementata per predicato: (ANY ProjID CONTAINS [cd]" 10000085 ")'" – HurkNburkS

+0

"ANY ProjID CONTAINS [cd] ..." non è il predicato che hai mostrato nella tua domanda ... –

risposta

16

Import NSManagedObject (InstallProject) a prendere un oggetto come questo,

-(InstallProject *)readSelectedInstall:(NSString *)projIDString 
{ 

    NSArray *fetchedObjects; 
    NSManagedObjectContext *context = [self managedObjectContext]; 
    NSFetchRequest *fetch = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"InstallProject" inManagedObjectContext: context]; 
    [fetch setEntity:entityDescription]; 
    [fetch setPredicate:[NSPredicate predicateWithFormat:@"(ANY ProjID contains[cd] %@)",projIDString]]; 
    NSError * error = nil; 
    fetchedObjects = [context executeFetchRequest:fetch error:&error]; 

    if([fetchedObjects count] == 1) 
    return [fetchedObjects objectAtIndex:0]; 
    else 
    return nil; 

} 
+0

ok, ho capito che non stavo inizializzando la mia lezione di coredataging e dopo averlo fatto questo è il codice restituito "Terminazione dell'app a causa dell'eccezione non rilevata 'NSInvalidArgumentException', motivo: 'generazione SQL non implementata per predicato: (ANY ProjID CONTAINS [cd]" 10000085 ") '" – HurkNburkS

+0

okay usa direttamente [NSPredicate predicateWithFormat: @ "ProjID ==% @", projIDString] o prova, [NSPredicate predicateWithFormat: @ "ProjID CONTAINS% @", name]]; – karthika

+0

È necessario sostituire IF ELSE alla fine e semplicemente restituire [fetchedObjects firstObject]. Se non ci sono oggetti nell'array, restituirà zero per te. – CharleyXIV

11

Che dire del fetchLimit sul fetchRequest È possibile impostare che a

La prendere limite specifica al massimo numero di oggetti che una richiesta dovrebbe restituire quando viene eseguita.

Es. In Swift:

let fetchRequest = NSFetchRequest(entityName: "Cart") 
fetchRequest.fetchLimit = 1 
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)] 

Edit: I sortDescriptors sono impostate solo perché stavo usando questo fetchRequest con una NFRC, si può solo eseguire il fetchRequest troppo:

do { 
    let fetchRequest = NSFetchRequest(entityName: "Cart") 
    fetchRequest.fetchLimit = 1 
    fetchRequest.predicate = NSPredicate(format: "name == %@", mainCart) 
    var objects: [Cart] 
    try objects = mainMoc.executeFetchRequest(fetchRequest) as! [Cart] 
} catch { 

} 
Problemi correlati