entidad de obtención de datos central durante la enumeración de la matriz

I am trying to enumerate through an array of tagged objects, each object has a corresponding core data entity with a "tag" attribute that I use as a predicate to fetch the correct entity for the enumerated object. See code below.

This seems to be causing some problems, as I am updating the entity in the enumeration, and I suspect that the problem is that the fetch is slower than the enumeration. How could I enumerate through this array and update fetch entities properly?

[array enumerateObjectsUsingBlock:^(obj *SomeClass, NSUInteger idx, BOOL *stop){

    currentEntityForEnumeratedObject = [targetVC fetchEntityForTag:obj.tag createIfNeccessary:NO error:nil];

    currentEntityForEnumeratedObject.someAttribute = [NSNumber numberWithInt:obj.somePropertyOfObj];

}];

preguntado el 22 de septiembre de 13 a las 04:09

In first place : what happens if currentEntityForEnumeratedObject is not fetch you returns nil ? Two : enumerateObjectsUsingBlock is a synchronous execution it's in fetchEntityForTag that you called the backend ? -

1 Respuestas

As a principle, you cannot update objects through which you are enumerating. From the documentación:

You cannot mutate a collection during fast enumeration, even if the collection is mutable. If you attempt to add or remove a collected object from within the loop, you’ll generate a runtime exception.

Also, it seems to me that doing a fetch in an enumeration loop is suboptimal design. Rather, you should fetch all your objects and then iterate through them to modify them. You can use a special predicate syntax to get all records that have a certain attribute in a collection:

// make a collection of all the tags of the *SomeClass objects
NSArray *tags = [array objectForKeyPath:@"tag"];

// fetch all relevant records with this predicate
[NSPredicate predicateWithFormat:@"tag in %@", tags];

Respondido el 22 de Septiembre de 13 a las 10:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.