From be65087d8b4390f259844e59ef579584bbee2048 Mon Sep 17 00:00:00 2001 From: StuFF mc Date: Tue, 15 Jan 2013 19:20:20 +0100 Subject: [PATCH 1/2] Fix https://alpha.app.net/tonymillion/post/2397422 --- .../NSManagedObject+MagicalDataImport.m | 52 +++++++++++++------ 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalDataImport.m b/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalDataImport.m index 368bba8..fafbb29 100644 --- a/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalDataImport.m +++ b/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalDataImport.m @@ -259,24 +259,44 @@ NSString * const kMagicalRecordImportAttributeUseDefaultValueWhenNotPresent = @" + (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData inContext:(NSManagedObjectContext *)context { - NSMutableArray *objectIDs = [NSMutableArray array]; +// NSMutableArray *objectIDs = [NSMutableArray array]; +// +// [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) +// { +// [listOfObjectData enumerateObjectsWithOptions:0 usingBlock:^(id obj, NSUInteger idx, BOOL *stop) +// { +// NSDictionary *objectData = (NSDictionary *)obj; +// +// NSManagedObject *dataObject = [self MR_importFromObject:objectData inContext:localContext]; +// +// if ([context obtainPermanentIDsForObjects:[NSArray arrayWithObject:dataObject] error:nil]) +// { +// [objectIDs addObject:[dataObject objectID]]; +// } +// }]; +// }]; +// +// return [self MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"self IN %@", objectIDs] inContext:context]; - [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) - { - [listOfObjectData enumerateObjectsWithOptions:0 usingBlock:^(id obj, NSUInteger idx, BOOL *stop) - { - NSDictionary *objectData = (NSDictionary *)obj; - - NSManagedObject *dataObject = [self MR_importFromObject:objectData inContext:localContext]; - - if ([context obtainPermanentIDsForObjects:[NSArray arrayWithObject:dataObject] error:nil]) - { - [objectIDs addObject:[dataObject objectID]]; - } - }]; - }]; - return [self MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"self IN %@", objectIDs] inContext:context]; + // See https://gist.github.com/4501089 and https://alpha.app.net/tonymillion/post/2397422 + + NSMutableArray *objects = [NSMutableArray array]; + + [listOfObjectData enumerateObjectsWithOptions:0 + usingBlock:^(id obj, NSUInteger idx, BOOL *stop) + { + NSDictionary * dict = obj; + + if([dict isKindOfClass:[NSDictionary class]]) + { + NSManagedObject *importedObject = [self MR_importFromObject:dict + inContext:context]; + [objects addObject:importedObject]; + } + }]; + + return objects; } @end From d3fc9fc84b1aca11c8f0298bde459113f3597611 Mon Sep 17 00:00:00 2001 From: StuFF mc Date: Thu, 24 Jan 2013 00:17:13 +0100 Subject: [PATCH 2/2] ability to use dateFormat.1 and so on as a failover --- .../NSAttributeDescription+MagicalDataImport.m | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/MagicalRecord/Categories/DataImport/NSAttributeDescription+MagicalDataImport.m b/MagicalRecord/Categories/DataImport/NSAttributeDescription+MagicalDataImport.m index af10f06..a5d05e2 100644 --- a/MagicalRecord/Categories/DataImport/NSAttributeDescription+MagicalDataImport.m +++ b/MagicalRecord/Categories/DataImport/NSAttributeDescription+MagicalDataImport.m @@ -36,8 +36,19 @@ { if (![value isKindOfClass:[NSDate class]]) { - NSString *dateFormat = [[self userInfo] valueForKey:kMagicalRecordImportCustomDateFormatKey]; - value = dateFromString([value description], dateFormat ?: kMagicalRecordImportDefaultDateFormatString); + NSDate *convertedValue = nil; + NSString *dateFormat; + NSUInteger index = 0; + do { + NSMutableString *dateFormatKey = [kMagicalRecordImportCustomDateFormatKey mutableCopy]; + if (index) { + [dateFormatKey appendFormat:@".%d", index]; + } + index++; + dateFormat = [[self userInfo] valueForKey:dateFormatKey]; + convertedValue = dateFromString([value description], dateFormat ?: kMagicalRecordImportDefaultDateFormatString); + } while (!convertedValue && dateFormat); + value = convertedValue; } // value = adjustDateForDST(value); }