Here is my use case:
I need to export a large core data store into some format (e.g., CSV, JSON) which requires fetching all objects of the main entity, and then iterating each object and serializing it to the desired format. Here is my code:
Since the for-loop code is running synchronously and in a single thread, it might take a long time to complete. This is especially true when dealing with a large database containing thousands of records.
I wonder if there is a way to iterate the array concurrently, in a way that will take full advantage of the multiple cores available on the iOS device (rather than using only a single core). This would probably boost the performance significantly.
I was thinking in the direction of using the following code to replace the for-loop code above:
However, this would obviously crash the app due to violating core data concurrency rules...
Any ideas how to target this use case?
I need to export a large core data store into some format (e.g., CSV, JSON) which requires fetching all objects of the main entity, and then iterating each object and serializing it to the desired format. Here is my code:
Code Block NSError *error = nil;NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"MyEntity"];NSArray<NSManagedObject *> *allItems = [managedObjectContext executeFetchRequest:request error:&error];for (NSManagedObject *item in allItems) { [self exportItem:item];}
Since the for-loop code is running synchronously and in a single thread, it might take a long time to complete. This is especially true when dealing with a large database containing thousands of records.
I wonder if there is a way to iterate the array concurrently, in a way that will take full advantage of the multiple cores available on the iOS device (rather than using only a single core). This would probably boost the performance significantly.
I was thinking in the direction of using the following code to replace the for-loop code above:
Code Block [allItems enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSManagedObject* item) { [self exportItem:item]; }
However, this would obviously crash the app due to violating core data concurrency rules...
Any ideas how to target this use case?