How to check empty records in Core Data?

Asked

Viewed 127 times

0

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

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

But I wonder if Core Data has a method or function that provides this, returning an array with the failed records.

Entity Example with failed fields :

 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);
  }];
}
  • You can check before you save them. In fact before you even turn them into NSManagedObject

  • Yes I was thinking about this alternative, I thought about calling the verification method in the block executed at the time of failure in Afnetworking.

  • It is much easier for you to check before assigning and saving than you assign, save, then check, and update incorrect or missing information. This still prevents any crash (depending on how you use the data later)

  • Another thing, I find it kind of unlikely that incomplete data will return to you, usually, even in bad connections what happens is it takes time to complete the return and if by chance there are losses, the same boom fails. If any data is returning with missing information there is a good chance it is in the information that is returning already from within the web service or even in converting this information.

  • I did timeout tests and noticed that even if Json comes with a failure the app can capture some data and register in coredata normally, but not all items are filled normally (the ones that failed). That’s why I think it’s more logical to check the consistency in the bank than at the time they’re coming.

  • could post your code? Normally when using requests I hope to receive all the data so convert into objects to save them. If you can post your code it will be easier to assist you

  • My code is a basic implementation of Afnetworking. I’ll post it for you to see...

  • The image request is made separately, and as it comes to a long file it may not be fully downloaded, for several errors. Then the IMAGE NOMEMAGE column in Coredata would be blank. That’s why the check should be in Core Data and not at the moment I’m still getting the image URL. I am doing core data matching at 2 different times. First the data and then only images. I save the image to disk and put its name in Core Data.

Show 3 more comments

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];

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

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

  • Vlw! I thought it would be using some specific method!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.