R
R
Ruslan Bergutov2015-11-05 08:33:57
Objective-C
Ruslan Bergutov, 2015-11-05 08:33:57

Why does the code stop after deleting objects from Core Data?

TTNetworkDataSource* netSource = [TTNetworkDataSource sharedSource];
        [netSource setContext:self];

        TTDataSourceResponse* districtsResponse = [netSource requestDataWithQuery:@{@(OBJECT_TYPE): [HotlineDistrict clsName]}];


        if (districtsResponse != nil) {
            NSArray* objects = [self executeFetchRequestUnsafe:[NSFetchRequest fetchRequestWithEntityName:[HotlineDistrict clsName]] error:&localError];

            NSInteger i = 0;

            if (localError != nil) {
                result = NO; return;
            } else {
                NSLog(@"%lu = fetchedObjects, %lu = districtsResponse", (unsigned long)objects.count, (unsigned long)districtsResponse.objects.count);
                for (NSManagedObject* obj in objects) {
                    NSLog(@"Deleted Objects in fetched objects %ld", (unsigned long)i++);
                    [self deleteObject:obj];
                }

                for (NSManagedObject* obj in districtsResponse.objects) {
                    NSLog(@"Inserted Object in districtsResponse %ld", (unsigned long)i++);
                    [self insertObject:obj];
                }
            }
        } else {
            localError = [NSError errorInfo:@{NSLocalizedDescriptionKey: @"Error when getting districts."}];
            result = NO;
        }

        TTDataSourceResponse* requestTypeResponse = [netSource requestDataWithQuery:@{@(OBJECT_TYPE): [HotlineRequestType clsName]}];


        if (requestTypeResponse != nil) {
            NSArray* objects = [self executeFetchRequestUnsafe:[NSFetchRequest fetchRequestWithEntityName:[HotlineRequestType clsName]] error:&localError];

            NSInteger j = 0;

            if (localError != nil) {
                result = NO; return;
            } else {
                NSLog(@"%lu = fetchedObjects, %lu = requestTypeResponse", (unsigned long)objects.count, (unsigned long)requestTypeResponse.objects.count);
                for (NSManagedObject* obj in objects) {
                    NSLog(@"Deleted Objects in fetched objects %ld", (unsigned long)j++);
                    [self deleteObject:obj];
                }

                for (NSManagedObject* obj in requestTypeResponse.objects) {
                    NSLog(@"Deleted Objects in requestTypeResponse %ld", (unsigned long)j++);
                    [self insertObject:obj];
                }
            }
        } else {
            localError = [NSError errorInfo:@{NSLocalizedDescriptionKey: @"Error when getting request types."}];
            result = NO;
        }

There is such a code, after passing the loop with the line [self deleteObject: obj] the application freezes.
Hand-to-hand debugging in NSLog'ov gives the following results:
NSLog(@"%lu = fetchedObjects, %lu = districtsResponse", (unsigned long)objects.count, (unsigned long)districtsResponse.objects.count);
gives out:
802 = fetchedObjects, 47 = districtsResponse

NSLog'i in cycles issue an enumeration of objects, as a result, the sum of fetchedObjects and districtsResponse is obtained, even if you put NSLog at the end of the cycle body, which indicates that the cycle is being executed. However, the application freezes. There are 2 such cycles, the second one does not reach, if you delete the lines of the cycle with deletion - everything works.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question