How to check empty records in Core Data?

0

I'm working on a way to know if data has been downloaded and recorded correctly on Core Data.  Due to the poor quality of 3G connections and the potential for failure, I would like to know if there is a way to query the core data and see if there is any way to return the empty records in an array.

The initial idea I had is to get the records in the entities and scan them for fields with NULL values. And then create a request to populate the data correctly.

But I would like to know if Core Data has a method or function that does this, returning an array with the failed records.

Example Entity with fields filled in failed:

 ID | NOME  | VALOR | IMAGEM 
-------------------------------
  0 | arroz | NULL  | skms.png
-------------------------------
  1 | feijao| NULL  | ksle.png
-------------------------------
  2 | carne | 5,00  | NULL
-------------------------------
  3 | uva   | 6,00  | NULL
-------------------------------
  4 | pera  | 7,00  | lskx.png
-------------------------------
  5 | NULL  | NULL  | NULL
-------------------------------



// Codigo usado para Download
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
    operationManager.responseSerializer = [AFJSONResponseSerializer serializer];
    [operationManager.requestSerializer setValue:urlGlobalToken forHTTPHeaderField:urlGlobalHttpHeader];

    [operationManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSMutableArray* arrayDataReceived =  [responseObject objectForKey:@"data"];

        cleanArray = [[NSMutableArray alloc]init];
        arrayURLImagens = [[NSMutableArray alloc]init];

    for ( NSMutableDictionary* valuesDict in arrayDataReceived) {

        // Este modelo é somente para exemplo.
        // This model is only for example.

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

        [cleanDict setObject: [valuesDict objectForKey:@"id"]         forKey:@"id"];
        [cleanDict setObject: [valuesDict objectForKey:@"nome"]       forKey:@"nome"];
        [cleanDict setObject: [valuesDict objectForKey:@"data"]       forKey:@"data"];
        [cleanDict setObject: [valuesDict objectForKey:@"descricao"]  forKey:@"descricao"];
        [cleanDict setObject: [valuesDict objectForKey:@"updated_at"] forKey:@"updateat"];

        // Imagens para update
        [cleanDict setObject: [valuesDict objectForKey:@"imagem"]forKey:@"urlimagem"];
    // Armazena os dicionarios em um Array
    [cleanArray addObject:cleanDict];
}  
    _dadosBrutosRecebidos = [[NSMutableDictionary alloc]init];
    [_dadosBrutosRecebidos setObject:arrayURLImagens forKey:@"urlimagem"];
    [_dadosBrutosRecebidos setObject:cleanArray forKey:@"strings"];

    [self.delegate performSelector:@selector(downloadDadosEventosCompleto)];

     app.networkActivityIndicatorVisible = NO;

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    app.networkActivityIndicatorVisible = NO;
    NSLog(@"failure. Error:%@. URL ERRO: %@",error, url);
  }];
}
    
asked by anonymous 06.02.2015 / 18:29

1 answer

1

You can instruct CoreData to return the object that has the "nil" properties.

Assuming the entity of your example the following request will return all entities that have one of the columns (name, value, image) as nil :

NSManagedObjectContext *context = <Pega o contexto do seu CoreData aqui>;
NSEntityDescription *exemploEntityDescription = [NSEntityDescription entityForName:@"Exemplo" inManagedObjectContext:contexto];
NSPredicate *dadosIncompletoPredicate = [NSPredicate predicateWithFormat:@"(name == nil) OR (valor == nil) OR (imagem == nil)"];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = exemploEntityDescription;
request.predicate = dadosIncompletoPreditate;

NSArray *entidatesComCamposNulos = [context executeFetchRequest:request error:nil];

So just check if the array is empty and different from nil and then do what you need to ensure the consistency of your data.

I also strongly recommend that you do not put a "column" with the name "ID" in your CoreData, id is reserved word of the language and can lead to problems in the future.

    
12.04.2015 / 21:12