xemacobra
xemacobra

Reputation: 1954

(CoreData) MagicalRecord suddenly stops saving persistently

I'm working on an iOS project in which huge amounts of data need to be downloaded from the internet and saved in a database. I'm using MagicalRecord to handle CoreData.

It works great for a while. Then, around the time when the sqlite database reaches around 3.6-4.0 MB it suddenly stops saving the information. That is in the same run with no apparent reason. The console printing just keeps going on as if there is no issues.

I'm new to MagicalRecord and CoreData in general. Not sure what the issue is. Thanks

This is the code that is responsible for the collection:

+ (void)downloadObjects {
    NSString *urlString = [NSString stringWithFormat:@"http://url.to.backend/server?with=parameters"];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        // Parse the XML into a dictionary
        NSError *parseError = nil;
        NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLParser:(NSXMLParser *)responseObject error:&parseError];
        __block NSArray *objectsArray = xmlDictionary[@"list"][@"object"];

        [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
            NSLog(@"PROCESSING: %d objects", [objectsArray count]);

            if (objectsArray.count) {
                for (NSDictionary *object in objectsArray) {
                    long long objectID = [object[@"objectid"] longLongValue];
                    __block NSString *latitude = object[@"latitude"];
                    __block NSString *longitude = object[@"longitude"];
                    __block NSString *speed = object[@"speed"];
                    __block NSString *distance = object[@"distance"];
                    __block NSDate *time = object[@"time"];

                    [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
                        if (time) { //Check if valid object
                            Object *existingObject = [ObjectManager objectWithID:objectID];
                            if (!existingObject) { //Check if existing object
                                if (DEBUG_DATA_COLLECTION) NSLog(@"%lld: NEW OBJECT", objectID);

                                Object *anObject = [Object MR_createEntity];

                                anObject.time = time;
                                anObject.objectID = @(objectID);
                                if (distance) anObject.distance = @([distance floatValue]);
                                if (latitude) anObject.latitude = @([latitude floatValue]);
                                if (longitude) anObject.longitude = @([longitude floatValue]);
                                if (speed) anObject.speed = @([speed floatValue]);

                            }
                            else {
                                if (DEBUG_DATA_COLLECTION) NSLog(@"%lld: EXISTING OBJECT", objectID);
                            }
                        }
                    }];
                }
            }

            [ObjectManager aggregatePerDay];
            [ObjectManager aggregatePerMonth];
        } completion:^(BOOL success, NSError *error) {
            NSLog(@"COMPLETED: %d objects", [objectsArray count]);
            NSLog(@"---------------------");
        }];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

Upvotes: 0

Views: 189

Answers (1)

xemacobra
xemacobra

Reputation: 1954

After 3 days of debugging and retracing my steps, I finally figured out what the issue is.

It had nothing to do with CoreData or MagicalRecord.

One of the objects I was getting from the server had speed = (null). This basically, stopped all entries in the database from that object and beyond instead of just failing and working for the rest. It might be because I was using saveWithBlockAndWait: instead of just saveWithBlock: but I'm not experienced enough to say for sure.

Upvotes: 1

Related Questions