Skip to content
Browse files

Merge pull request #1 from pandamonia/master

Hopefully some useful updates from @pandamonia.
  • Loading branch information...
2 parents d16990b + 606705a commit 53e6f3b2000a219241fb9df3d4870926326575d9 @zwaldowski zwaldowski committed
Showing with 1,367 additions and 1,329 deletions.
  1. +8 −7 Mac App Unit Tests/Mac App Unit Tests-Prefix.pch
  2. +28 −18 Magical Record.xcodeproj/project.pbxproj
  3. +50 −41 Source/Categories/NSManagedObject+MagicalDataImport.h
  4. +210 −210 Source/Categories/NSManagedObject+MagicalDataImport.m
  5. +188 −188 Source/Categories/NSManagedObject+MagicalRecord.m
  6. +62 −62 Source/Categories/NSManagedObjectContext+MagicalRecord.m
  7. +13 −13 Source/Categories/NSManagedObjectModel+MagicalRecord.m
  8. +17 −17 Source/Categories/NSPersistentStore+MagicalRecord.m
  9. +79 −79 Source/Categories/NSPersistentStoreCoordinator+MagicalRecord.m
  10. +3 −3 Source/MRCoreDataAction.h
  11. +39 −39 Source/MRCoreDataAction.m
  12. +2 −2 Source/MagicalRecordHelpers.h
  13. +140 −134 Source/MagicalRecordHelpers.m
  14. +1 −1 Unit Tests/Fixtures/FixtureHelpers.h
  15. +17 −17 Unit Tests/Fixtures/FixtureHelpers.m
  16. +42 −42 Unit Tests/Fixtures/ImportSingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeysTests.m
  17. +7 −7 Unit Tests/ImportSingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKeyTests.m
  18. +30 −30 Unit Tests/ImportSingleEntityRelatedToMappedEntityUsingDefaultsTests.m
  19. +66 −66 Unit Tests/ImportSingleEntityRelatedToMappedEntityUsingMappedPrimaryKeyTests.m
  20. +8 −8 Unit Tests/ImportSingleEntityRelatedToMappedEntityWithNestedMappedAttributesTests.m
  21. +7 −7 Unit Tests/ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m
  22. +49 −49 Unit Tests/ImportSingleEntityWithNoRelationshipsTests.m
  23. +37 −37 Unit Tests/ImportSingleRelatedEntityTests.m
  24. +6 −2 Unit Tests/MagicalDataImportTestCase.h
  25. +13 −13 Unit Tests/MagicalDataImportTestCase.m
  26. +3 −3 Unit Tests/MagicalRecordHelperTests.h
  27. +84 −75 Unit Tests/MagicalRecordHelperTests.m
  28. +1 −1 Unit Tests/NSManagedObjectContextHelperTests.h
  29. +7 −7 Unit Tests/NSManagedObjectContextHelperTests.m
  30. +1 −1 Unit Tests/NSManagedObjectHelperTests.h
  31. +47 −47 Unit Tests/NSManagedObjectHelperTests.m
  32. +1 −1 Unit Tests/NSManagedObjectModelHelperTests.h
  33. +1 −1 Unit Tests/NSManagedObjectModelHelperTests.m
  34. +1 −1 Unit Tests/NSPersisentStoreHelperTests.h
  35. +61 −62 Unit Tests/NSPersisentStoreHelperTests.m
  36. +1 −1 Unit Tests/NSPersistentStoreCoordinatorHelperTests.h
  37. +26 −26 Unit Tests/NSPersistentStoreCoordinatorHelperTests.m
  38. +11 −11 iOS App Unit Tests/iOS App Unit Tests-Prefix.pch
View
15 Mac App Unit Tests/Mac App Unit Tests-Prefix.pch
@@ -3,13 +3,14 @@
//
#ifdef __OBJC__
- #import <Cocoa/Cocoa.h>
- #import <GHUnit/GHUnit.h>
+ #import <Cocoa/Cocoa.h>
+ #import <GHUnit/GHUnit.h>
+ #import <OCMock/OCMock.h>
- #define HC_SHORTHAND
- #import <OCHamcrest/OCHamcrest.h>
+ #define HC_SHORTHAND
+ #import <OCHamcrest/OCHamcrest.h>
- #define MR_SHORTHAND
- #import "CoreData+MagicalRecord.h"
- #import "FixtureHelpers.h"
+ #define MR_SHORTHAND
+ #import "CoreData+MagicalRecord.h"
+ #import "FixtureHelpers.h"
#endif
View
46 Magical Record.xcodeproj/project.pbxproj
@@ -7,6 +7,10 @@
objects = {
/* Begin PBXBuildFile section */
+ 000C4315146F86FD006174F3 /* OCMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C721C84313D0C6460097AB6F /* OCMock.framework */; };
+ 000C4316146F8931006174F3 /* MagicalDataImportTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = C7913B9C13FAFC13007E09CC /* MagicalDataImportTestCase.m */; };
+ 0031017A146F758F00AA9A2F /* NSManagedObject+MagicalDataImport.m in Sources */ = {isa = PBXBuildFile; fileRef = 00310179146F758F00AA9A2F /* NSManagedObject+MagicalDataImport.m */; };
+ 0031017B146F758F00AA9A2F /* NSManagedObject+MagicalDataImport.m in Sources */ = {isa = PBXBuildFile; fileRef = 00310179146F758F00AA9A2F /* NSManagedObject+MagicalDataImport.m */; };
C70B6E7113D0F62500709450 /* NSPersisentStoreHelperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C70B6E7013D0F62500709450 /* NSPersisentStoreHelperTests.m */; };
C70B6E7413D0F64000709450 /* NSPersistentStoreCoordinatorHelperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C70B6E7313D0F64000709450 /* NSPersistentStoreCoordinatorHelperTests.m */; };
C70B6E7713D0F66000709450 /* NSManagedObjectModelHelperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C70B6E7613D0F66000709450 /* NSManagedObjectModelHelperTests.m */; };
@@ -37,9 +41,9 @@
C721C87713D0C7030097AB6F /* MagicalRecordHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = C721C86713D0C7030097AB6F /* MagicalRecordHelpers.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
C738127A1410398A0054EEF0 /* ImportSingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeysTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C73812781410383B0054EEF0 /* ImportSingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeysTests.m */; };
C738127B141039900054EEF0 /* SingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeys.json in Resources */ = {isa = PBXBuildFile; fileRef = C7381275141037E80054EEF0 /* SingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeys.json */; };
- C753897413DB61CE002B2F57 /* GHUnitTestMain.m in Sources */ = {isa = PBXBuildFile; fileRef = C753897313DB61CE002B2F57 /* GHUnitTestMain.m */; };
+ C753897413DB61CE002B2F57 /* GHUnitTestMain.m in Sources */ = {isa = PBXBuildFile; fileRef = C753897313DB61CE002B2F57 /* GHUnitTestMain.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
C753897613DB6322002B2F57 /* GHUnit.framework in Copy GHUnit into App Bundle */ = {isa = PBXBuildFile; fileRef = C721C84113D0C6460097AB6F /* GHUnit.framework */; };
- C763783213E10BEC0009A6CA /* GHUnitIOSTestMain.m in Sources */ = {isa = PBXBuildFile; fileRef = C763783113E10BEC0009A6CA /* GHUnitIOSTestMain.m */; };
+ C763783213E10BEC0009A6CA /* GHUnitIOSTestMain.m in Sources */ = {isa = PBXBuildFile; fileRef = C763783113E10BEC0009A6CA /* GHUnitIOSTestMain.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
C76AF7E513DBC08F00CE2E05 /* FixtureHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = C77E5FBA13D0D2AE00298F87 /* FixtureHelpers.m */; };
C76AF7E613DBC08F00CE2E05 /* MagicalRecordHelperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C77E5FA713D0CBDE00298F87 /* MagicalRecordHelperTests.m */; };
C76AF7E713DBC08F00CE2E05 /* ImportSingleEntityWithNoRelationshipsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C77E5FB713D0D25100298F87 /* ImportSingleEntityWithNoRelationshipsTests.m */; };
@@ -52,7 +56,6 @@
C76AF7EE13DBC09B00CE2E05 /* SingleEntityWithNoRelationships.plist in Resources */ = {isa = PBXBuildFile; fileRef = C77E5FB213D0D1D100298F87 /* SingleEntityWithNoRelationships.plist */; };
C76AF7F213DBC12500CE2E05 /* OCHamcrest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C721C84213D0C6460097AB6F /* OCHamcrest.framework */; };
C76AF7F413DBC34300CE2E05 /* OCHamcrest.framework in Copy OCHamcrest into App Bundle */ = {isa = PBXBuildFile; fileRef = C721C84213D0C6460097AB6F /* OCHamcrest.framework */; };
- C76AF7FB13DBEB5500CE2E05 /* ImportSingleRelatedEntityTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C76AF7FA13DBEB5500CE2E05 /* ImportSingleRelatedEntityTests.m */; };
C76AF7FC13DBEB5500CE2E05 /* ImportSingleRelatedEntityTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C76AF7FA13DBEB5500CE2E05 /* ImportSingleRelatedEntityTests.m */; };
C76AF82A13DBEE5A00CE2E05 /* SingleRelatedEntity.json in Resources */ = {isa = PBXBuildFile; fileRef = C76AF82913DBEE5A00CE2E05 /* SingleRelatedEntity.json */; };
C76AF82B13DBEE5A00CE2E05 /* SingleRelatedEntity.json in Resources */ = {isa = PBXBuildFile; fileRef = C76AF82913DBEE5A00CE2E05 /* SingleRelatedEntity.json */; };
@@ -151,6 +154,8 @@
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
+ 00310178146F758F00AA9A2F /* NSManagedObject+MagicalDataImport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSManagedObject+MagicalDataImport.h"; sourceTree = "<group>"; };
+ 00310179146F758F00AA9A2F /* NSManagedObject+MagicalDataImport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSManagedObject+MagicalDataImport.m"; sourceTree = "<group>"; };
C70B6E6F13D0F62500709450 /* NSPersisentStoreHelperTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSPersisentStoreHelperTests.h; path = "Unit Tests/NSPersisentStoreHelperTests.h"; sourceTree = "<group>"; };
C70B6E7013D0F62500709450 /* NSPersisentStoreHelperTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NSPersisentStoreHelperTests.m; path = "Unit Tests/NSPersisentStoreHelperTests.m"; sourceTree = "<group>"; };
C70B6E7213D0F64000709450 /* NSPersistentStoreCoordinatorHelperTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSPersistentStoreCoordinatorHelperTests.h; path = "Unit Tests/NSPersistentStoreCoordinatorHelperTests.h"; sourceTree = "<group>"; };
@@ -167,12 +172,12 @@
C721C7E313D0C3A00097AB6F /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
C721C7E613D0C3A00097AB6F /* Mac App Unit Tests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Mac App Unit Tests-Info.plist"; sourceTree = "<group>"; };
C721C7E813D0C3A00097AB6F /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
- C721C7EC13D0C3A00097AB6F /* Mac App Unit Tests-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Mac App Unit Tests-Prefix.pch"; sourceTree = "<group>"; };
+ C721C7EC13D0C3A00097AB6F /* Mac App Unit Tests-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = "Mac App Unit Tests-Prefix.pch"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
C721C7EE13D0C3A00097AB6F /* en */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; name = en; path = en.lproj/Credits.rtf; sourceTree = "<group>"; };
C721C7FD13D0C3CD0097AB6F /* iOS App Unit Tests.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "iOS App Unit Tests.app"; sourceTree = BUILT_PRODUCTS_DIR; };
C721C80413D0C3CD0097AB6F /* iOS App Unit Tests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "iOS App Unit Tests-Info.plist"; sourceTree = "<group>"; };
C721C80613D0C3CD0097AB6F /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
- C721C80A13D0C3CD0097AB6F /* iOS App Unit Tests-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "iOS App Unit Tests-Prefix.pch"; sourceTree = "<group>"; };
+ C721C80A13D0C3CD0097AB6F /* iOS App Unit Tests-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = "iOS App Unit Tests-Prefix.pch"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
C721C82F13D0C6390097AB6F /* GHUnitIOS.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = GHUnitIOS.framework; sourceTree = "<group>"; };
C721C83013D0C6390097AB6F /* OCHamcrestIOS.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = OCHamcrestIOS.framework; sourceTree = "<group>"; };
C721C83113D0C6390097AB6F /* OCMock.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = OCMock.framework; sourceTree = "<group>"; };
@@ -204,24 +209,24 @@
C721C86113D0C7030097AB6F /* NSPersistentStore+MagicalRecord.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSPersistentStore+MagicalRecord.h"; sourceTree = "<group>"; };
C721C86213D0C7030097AB6F /* NSPersistentStore+MagicalRecord.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSPersistentStore+MagicalRecord.m"; sourceTree = "<group>"; };
C721C86313D0C7030097AB6F /* NSPersistentStoreCoordinator+MagicalRecord.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSPersistentStoreCoordinator+MagicalRecord.h"; sourceTree = "<group>"; };
- C721C86413D0C7030097AB6F /* NSPersistentStoreCoordinator+MagicalRecord.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSPersistentStoreCoordinator+MagicalRecord.m"; sourceTree = "<group>"; };
+ C721C86413D0C7030097AB6F /* NSPersistentStoreCoordinator+MagicalRecord.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = "NSPersistentStoreCoordinator+MagicalRecord.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
C721C86513D0C7030097AB6F /* CoreData+MagicalRecord.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "CoreData+MagicalRecord.h"; sourceTree = "<group>"; };
- C721C86613D0C7030097AB6F /* MagicalRecordHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MagicalRecordHelpers.h; sourceTree = "<group>"; };
+ C721C86613D0C7030097AB6F /* MagicalRecordHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = MagicalRecordHelpers.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
C721C86713D0C7030097AB6F /* MagicalRecordHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MagicalRecordHelpers.m; sourceTree = "<group>"; };
C7381275141037E80054EEF0 /* SingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeys.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = SingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeys.json; path = "Unit Tests/Fixtures/SingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeys.json"; sourceTree = "<group>"; };
- C73812781410383B0054EEF0 /* ImportSingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeysTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ImportSingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeysTests.m; path = "Unit Tests/Fixtures/ImportSingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeysTests.m"; sourceTree = "<group>"; };
+ C73812781410383B0054EEF0 /* ImportSingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeysTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; name = ImportSingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeysTests.m; path = "Unit Tests/Fixtures/ImportSingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeysTests.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
C753897313DB61CE002B2F57 /* GHUnitTestMain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GHUnitTestMain.m; sourceTree = "<group>"; };
C763783113E10BEC0009A6CA /* GHUnitIOSTestMain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GHUnitIOSTestMain.m; sourceTree = "<group>"; };
C76AF7F813DBE8D300CE2E05 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = text; path = README.md; sourceTree = "<group>"; };
- C76AF7FA13DBEB5500CE2E05 /* ImportSingleRelatedEntityTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ImportSingleRelatedEntityTests.m; path = "Unit Tests/ImportSingleRelatedEntityTests.m"; sourceTree = "<group>"; };
+ C76AF7FA13DBEB5500CE2E05 /* ImportSingleRelatedEntityTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; name = ImportSingleRelatedEntityTests.m; path = "Unit Tests/ImportSingleRelatedEntityTests.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
C76AF82913DBEE5A00CE2E05 /* SingleRelatedEntity.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = SingleRelatedEntity.json; path = "Unit Tests/Fixtures/SingleRelatedEntity.json"; sourceTree = "<group>"; };
C77E5F9C13D0CA1800298F87 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/System/Library/Frameworks/CoreGraphics.framework; sourceTree = DEVELOPER_DIR; };
C77E5F9F13D0CA2700298F87 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/System/Library/Frameworks/UIKit.framework; sourceTree = DEVELOPER_DIR; };
C77E5FA613D0CBDE00298F87 /* MagicalRecordHelperTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MagicalRecordHelperTests.h; path = "Unit Tests/MagicalRecordHelperTests.h"; sourceTree = "<group>"; };
- C77E5FA713D0CBDE00298F87 /* MagicalRecordHelperTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MagicalRecordHelperTests.m; path = "Unit Tests/MagicalRecordHelperTests.m"; sourceTree = "<group>"; };
+ C77E5FA713D0CBDE00298F87 /* MagicalRecordHelperTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; name = MagicalRecordHelperTests.m; path = "Unit Tests/MagicalRecordHelperTests.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
C77E5FB213D0D1D100298F87 /* SingleEntityWithNoRelationships.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = SingleEntityWithNoRelationships.plist; path = "Unit Tests/Fixtures/SingleEntityWithNoRelationships.plist"; sourceTree = "<group>"; };
C77E5FB413D0D1EC00298F87 /* SampleJSONDataForImport.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = SampleJSONDataForImport.json; path = "Unit Tests/Fixtures/SampleJSONDataForImport.json"; sourceTree = "<group>"; };
- C77E5FB713D0D25100298F87 /* ImportSingleEntityWithNoRelationshipsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ImportSingleEntityWithNoRelationshipsTests.m; path = "Unit Tests/ImportSingleEntityWithNoRelationshipsTests.m"; sourceTree = "<group>"; };
+ C77E5FB713D0D25100298F87 /* ImportSingleEntityWithNoRelationshipsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; name = ImportSingleEntityWithNoRelationshipsTests.m; path = "Unit Tests/ImportSingleEntityWithNoRelationshipsTests.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
C77E5FB913D0D2AE00298F87 /* FixtureHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FixtureHelpers.h; path = "Unit Tests/Fixtures/FixtureHelpers.h"; sourceTree = "<group>"; };
C77E5FBA13D0D2AE00298F87 /* FixtureHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FixtureHelpers.m; path = "Unit Tests/Fixtures/FixtureHelpers.m"; sourceTree = "<group>"; };
C784348B13F0FEE000463CEE /* _MappedEntity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _MappedEntity.h; sourceTree = "<group>"; };
@@ -244,10 +249,10 @@
C7913BA513FB1D17007E09CC /* _SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = _SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.m; sourceTree = "<group>"; };
C7913BA713FB1D2A007E09CC /* SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.h; sourceTree = "<group>"; };
C7913BA813FB1D2A007E09CC /* SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.m; sourceTree = "<group>"; };
- C7913BAC13FB1DF9007E09CC /* ImportSingleEntityRelatedToMappedEntityWithNestedMappedAttributesTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ImportSingleEntityRelatedToMappedEntityWithNestedMappedAttributesTests.m; path = "Unit Tests/ImportSingleEntityRelatedToMappedEntityWithNestedMappedAttributesTests.m"; sourceTree = "<group>"; };
+ C7913BAC13FB1DF9007E09CC /* ImportSingleEntityRelatedToMappedEntityWithNestedMappedAttributesTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; name = ImportSingleEntityRelatedToMappedEntityWithNestedMappedAttributesTests.m; path = "Unit Tests/ImportSingleEntityRelatedToMappedEntityWithNestedMappedAttributesTests.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
C7913BAE13FB1E11007E09CC /* SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.json; path = "Unit Tests/Fixtures/SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.json"; sourceTree = "<group>"; };
C7B7379913FAE5D500EE4940 /* SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.json; path = "Unit Tests/Fixtures/SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.json"; sourceTree = "<group>"; };
- C7B7379D13FAE6D000EE4940 /* ImportSingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKeyTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ImportSingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKeyTests.m; path = "Unit Tests/ImportSingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKeyTests.m"; sourceTree = "<group>"; };
+ C7B7379D13FAE6D000EE4940 /* ImportSingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKeyTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; name = ImportSingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKeyTests.m; path = "Unit Tests/ImportSingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKeyTests.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
C7BD885213DBF88F00274567 /* _AbstractRelatedEntity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _AbstractRelatedEntity.h; sourceTree = "<group>"; };
C7BD885313DBF88F00274567 /* _AbstractRelatedEntity.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = _AbstractRelatedEntity.m; sourceTree = "<group>"; };
C7BD885413DBF88F00274567 /* _ConcreteRelatedEntity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _ConcreteRelatedEntity.h; sourceTree = "<group>"; };
@@ -292,7 +297,7 @@
C7BD888B13DBFA6200274567 /* SingleRelatedEntity.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SingleRelatedEntity.m; sourceTree = "<group>"; };
C7C9A37113F43D93002C5B0C /* JSONKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = JSONKit.h; path = "Third Party/JSONKit.h"; sourceTree = "<group>"; };
C7C9A37213F43D93002C5B0C /* JSONKit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = JSONKit.m; path = "Third Party/JSONKit.m"; sourceTree = "<group>"; };
- C7C9A37513F44A3C002C5B0C /* ImportSingleEntityRelatedToMappedEntityUsingDefaultsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ImportSingleEntityRelatedToMappedEntityUsingDefaultsTests.m; path = "Unit Tests/ImportSingleEntityRelatedToMappedEntityUsingDefaultsTests.m"; sourceTree = "<group>"; };
+ C7C9A37513F44A3C002C5B0C /* ImportSingleEntityRelatedToMappedEntityUsingDefaultsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; name = ImportSingleEntityRelatedToMappedEntityUsingDefaultsTests.m; path = "Unit Tests/ImportSingleEntityRelatedToMappedEntityUsingDefaultsTests.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
C7C9A37C13F44B08002C5B0C /* SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey.json; path = "Unit Tests/Fixtures/SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey.json"; sourceTree = "<group>"; };
C7C9A37E13F44B29002C5B0C /* SingleEntityRelatedToMappedEntityUsingDefaults.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = SingleEntityRelatedToMappedEntityUsingDefaults.json; path = "Unit Tests/Fixtures/SingleEntityRelatedToMappedEntityUsingDefaults.json"; sourceTree = "<group>"; };
C7C9A38013F45240002C5B0C /* _SingleEntityRelatedToMappedEntityUsingDefaults.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _SingleEntityRelatedToMappedEntityUsingDefaults.h; sourceTree = "<group>"; };
@@ -303,7 +308,7 @@
C7C9A38713F4524B002C5B0C /* SingleEntityRelatedToMappedEntityUsingDefaults.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SingleEntityRelatedToMappedEntityUsingDefaults.m; sourceTree = "<group>"; };
C7C9A38813F4524B002C5B0C /* SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey.h; sourceTree = "<group>"; };
C7C9A38913F4524B002C5B0C /* SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey.m; sourceTree = "<group>"; };
- C7C9A38C13F45438002C5B0C /* ImportSingleEntityRelatedToMappedEntityUsingMappedPrimaryKeyTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ImportSingleEntityRelatedToMappedEntityUsingMappedPrimaryKeyTests.m; path = "Unit Tests/ImportSingleEntityRelatedToMappedEntityUsingMappedPrimaryKeyTests.m"; sourceTree = "<group>"; };
+ C7C9A38C13F45438002C5B0C /* ImportSingleEntityRelatedToMappedEntityUsingMappedPrimaryKeyTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; name = ImportSingleEntityRelatedToMappedEntityUsingMappedPrimaryKeyTests.m; path = "Unit Tests/ImportSingleEntityRelatedToMappedEntityUsingMappedPrimaryKeyTests.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
C7E736DE1402FE64005657C9 /* SingleEntityWithNoRelationships.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = SingleEntityWithNoRelationships.json; path = "Unit Tests/Fixtures/SingleEntityWithNoRelationships.json"; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -315,6 +320,7 @@
C721C7DF13D0C3A00097AB6F /* Cocoa.framework in Frameworks */,
C721C82413D0C45E0097AB6F /* Foundation.framework in Frameworks */,
C76AF7F213DBC12500CE2E05 /* OCHamcrest.framework in Frameworks */,
+ 000C4315146F86FD006174F3 /* OCMock.framework in Frameworks */,
C721C84E13D0C6460097AB6F /* GHUnit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -510,6 +516,8 @@
C721C85813D0C7030097AB6F /* Categories */ = {
isa = PBXGroup;
children = (
+ 00310178146F758F00AA9A2F /* NSManagedObject+MagicalDataImport.h */,
+ 00310179146F758F00AA9A2F /* NSManagedObject+MagicalDataImport.m */,
C721C85B13D0C7030097AB6F /* NSManagedObject+MagicalRecord.h */,
C721C85C13D0C7030097AB6F /* NSManagedObject+MagicalRecord.m */,
C721C85D13D0C7030097AB6F /* NSManagedObjectContext+MagicalRecord.h */,
@@ -868,7 +876,6 @@
C76AF7EA13DBC08F00CE2E05 /* NSManagedObjectModelHelperTests.m in Sources */,
C76AF7EB13DBC08F00CE2E05 /* NSManagedObjectContextHelperTests.m in Sources */,
C76AF7EC13DBC08F00CE2E05 /* NSManagedObjectHelperTests.m in Sources */,
- C76AF7FB13DBEB5500CE2E05 /* ImportSingleRelatedEntityTests.m in Sources */,
C7BD886813DBF88F00274567 /* _AbstractRelatedEntity.m in Sources */,
C7BD886913DBF88F00274567 /* _ConcreteRelatedEntity.m in Sources */,
C7BD886A13DBF88F00274567 /* _DifferentClassNameMapping.m in Sources */,
@@ -880,6 +887,8 @@
C7BD887013DBF88F00274567 /* SingleEntityWithNoRelationships.m in Sources */,
C7BD887113DBF88F00274567 /* SingleRelatedEntity.m in Sources */,
C7BD887213DBF88F00274567 /* TestModel.xcdatamodeld in Sources */,
+ 0031017A146F758F00AA9A2F /* NSManagedObject+MagicalDataImport.m in Sources */,
+ 000C4316146F8931006174F3 /* MagicalDataImportTestCase.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -935,6 +944,7 @@
C78F8FDA13FDC3F400549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.m in Sources */,
C78F8FDD13FDC3FD00549DD8 /* _SingleEntityRelatedToMappedEntityWithSecondaryMappings.m in Sources */,
C738127A1410398A0054EEF0 /* ImportSingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeysTests.m in Sources */,
+ 0031017B146F758F00AA9A2F /* NSManagedObject+MagicalDataImport.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -971,6 +981,7 @@
C721C7A713D0A3750097AB6F /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
+ CLANG_ENABLE_OBJC_ARC = YES;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
IPHONEOS_DEPLOYMENT_TARGET = 4.3;
MACOSX_DEPLOYMENT_TARGET = 10.7;
@@ -981,6 +992,7 @@
C721C7A813D0A3750097AB6F /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
+ CLANG_ENABLE_OBJC_ARC = YES;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
IPHONEOS_DEPLOYMENT_TARGET = 4.3;
MACOSX_DEPLOYMENT_TARGET = 10.7;
@@ -1080,7 +1092,6 @@
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
- CLANG_ENABLE_OBJC_ARC = NO;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
@@ -1135,7 +1146,6 @@
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
- CLANG_ENABLE_OBJC_ARC = NO;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES;
FRAMEWORK_SEARCH_PATHS = (
View
91 Source/Categories/NSManagedObject+MagicalDataImport.h
@@ -8,57 +8,66 @@
/** Importing for Core Data.
- These utilities do their best to safely, securely, and
- efficiently import Core Data objects using NSDictionary and
- NSArray to do the bulk of the work. This allows you to simply
- import to your data model from JSON, XML, or HTTP requests
- without worrying about finding and keeping track of
- existing objects yourself.
-
- On the whole, the update methods are slower because they
- always look for existing objects for relationships. However,
- it is only recommended to import when you are sure the new
- objects (and all relationships they contain) aren't already
- in the model, unless you plan on writing yourself a grand
- old garbage collection/duplicate resolution algorithm.
-
- Magical Record will use a number of user info keys on your
- entities, their attributes, and their relationships to import.
- They are as follows:
+ These utilities do their best to safely, securely, and efficiently import Core
+ Data objects using NSDictionary and NSArray to do the bulk of the work. This
+ allows you to simply import to your data model from JSON, XML, or HTTP requests
+ without worrying about finding and keeping track of existing objects yourself.
+
+ On the whole, the update methods are slower because they always look for
+ existing objects for relationships. However, it is only recommended to import
+ when you are sure the new objects (and all relationships they contain) aren't
+ already in the model, unless you plan on writing yourself a grand old garbage
+ collection/duplicate resolution algorithm.
+
+ MagicalRecord will use a number of user info keys on your entities, their
+ attributes, and their relationships to import. They are as follows:
*Entities*
- - `className`. The value of this is used to determine what key in
- the dictionary should be used to determine the class name/entity name
- of the model the dictionary correlates to. This is especially useful
- for importing sub-entities in relationships.
- - `primaryAttribute`. The value for this key is used for comparing
- and locating model objects. If no value is provided for this key,
- Magical Record will search for a property with the name xID, where
- x is the first letter of the entity name in lowercase.
+ - `className` (`kMagicalRecordImportClassNameKey`): The value of this is used
+ to determine what key in the dictionary should be used to determine the class
+ name/entity name of the model the dictionary correlates to. This is especially
+ useful for importing sub-entities in relationships.
+ - `primaryAttribute` (`kMagicalRecordImportPrimaryAttributeKey`): The value for
+ this key is used for comparing and locating model objects. If no value is
+ provided for this key, MagicalRecord will search for a property with the name
+ `xID`, where `x` is the first letter of the entity name in lowercase.
*Attributes*
- - `mappedKey`. The value of this is used to determine what a key
- in the dictionary should be inserted into the model as. To set the
- property "lastModify" on an entity using a dictionary that has a key
+ - `mappedKey` (`kMagicalRecordImportMapKey`): The value of this is used to
+ determine what a key in the dictionary should be inserted into the model as. To
+ set the property "lastModify" on an entity using a dictionary that has a key
"lastModifiedDate", set `mappedKey` to "lastModifiedDate" on that attribute.
- - `className`. Similar to its use at the entity level, this forces the
- class of the imported object into that of the value for this key. Note
- that it is recommended to use value transformers instead.
- - `dateFormat`. For a date attribute, Magical Record can automatically
- format a string into a date object. The value of this key is used in
- the date formatter. If it is not set, "yyyy-MM-dd'T'HH:mm:ss'Z'" is
- used by default.
+ - `className` (`kMagicalRecordImportClassNameKey`): Similar to its use at the
+ entity level, this forces the class of the imported object into that of the
+ value for this key. Note that it is recommended to use value transformers
+ instead.
+ - `dateFormat` (`kMagicalRecordImportCustomDateFormat`): For a date attribute,
+ MagicalRecord can automatically format a string into a date object. The value
+ of this key is used in the date formatter. If it is not set,
+ "yyyy-MM-dd'T'HH:mm:ss'Z'" (`kMagicalRecordImportDefaultDateFormat`) is used by
+ default.
*Relationships*
- - `mappedKey`. Same as for an attribute.
- - `primaryKey`. Compare to `primaryAttribute`. This
- key is used in relationships to define what objects
- to search for when associating different model objects
- using relationships in imported dictionaries.
- */
+ - `mappedKey` (`kMagicalRecordImportMapKey`): Same as for an attribute.
+ - `primaryKey` (`kMagicalRecordImportRelationshipPrimaryKey`): Compare to
+ `primaryAttribute`. This key is used in relationships to define what objects to
+ search for when associating different model objects using relationships in
+ imported dictionaries.
+
+ **/
+
+extern NSString * const kMagicalRecordImportCustomDateFormat;
+extern NSString * const kMagicalRecordImportDefaultDateFormat;
+
+extern NSString * const kMagicalRecordImportMapKey;
+extern NSString * const kMagicalRecordImportClassNameKey;
+
+extern NSString * const kMagicalRecordImportPrimaryAttributeKey;
+extern NSString * const kMagicalRecordImportRelationshipPrimaryKey;
+
@interface NSManagedObject (MagicalDataImport)
/** Imports values into a managed object by using
View
420 Source/Categories/NSManagedObject+MagicalDataImport.m
@@ -9,285 +9,285 @@
#import "CoreData+MagicalRecord.h"
#import <objc/message.h>
-static NSString * const kMagicalRecordImportCustomDateFormat = @"dateFormat";
-static NSString * const kMagicalRecordImportDefaultDateFormat = @"yyyy-MM-dd'T'HH:mm:ss'Z'";
+NSString * const kMagicalRecordImportCustomDateFormat = @"dateFormat";
+NSString * const kMagicalRecordImportDefaultDateFormat = @"yyyy-MM-dd'T'HH:mm:ss'Z'";
-static NSString * const kMagicalRecordImportMapKey = @"mappedKey";
-static NSString * const kMagicalRecordImportClassNameKey = @"className";
+NSString * const kMagicalRecordImportMapKey = @"mappedKey";
+NSString * const kMagicalRecordImportClassNameKey = @"className";
-static NSString * const kMagicalRecordImportPrimaryAttributeKey = @"primaryAttribute";
-static NSString * const kMagicalRecordImportRelationshipPrimaryKey = @"primaryKey";
+NSString * const kMagicalRecordImportPrimaryAttributeKey = @"primaryAttribute";
+NSString * const kMagicalRecordImportRelationshipPrimaryKey = @"primaryKey";
static NSString * attributeNameFromString(NSString *value)
{
- NSString *firstCharacter = [[value substringToIndex:1] capitalizedString];
- return [firstCharacter stringByAppendingString:[value substringFromIndex:1]];
+ NSString *firstCharacter = [[value substringToIndex:1] capitalizedString];
+ return [firstCharacter stringByAppendingString:[value substringFromIndex:1]];
}
static NSString *primaryKeyNameFromString(NSString *value)
{
- NSString *firstCharacter = [[value substringToIndex:1] lowercaseString];
- return [firstCharacter stringByAppendingFormat:@"%@ID", [value substringFromIndex:1]];
+ NSString *firstCharacter = [[value substringToIndex:1] lowercaseString];
+ return [firstCharacter stringByAppendingFormat:@"%@ID", [value substringFromIndex:1]];
}
@implementation NSManagedObject (MagicalDataImport)
- (NSManagedObject *)_createInstanceForEntity:(NSEntityDescription *)entityDescription withDictionary:(id)objectData
{
- NSManagedObject *relatedObject = [NSEntityDescription insertNewObjectForEntityForName:[entityDescription name]
- inManagedObjectContext:[self managedObjectContext]];
-
- [relatedObject importValuesFromDictionary:objectData];
-
- return relatedObject;
+ NSManagedObject *relatedObject = [NSEntityDescription insertNewObjectForEntityForName:[entityDescription name]
+ inManagedObjectContext:[self managedObjectContext]];
+
+ [relatedObject importValuesFromDictionary:objectData];
+
+ return relatedObject;
}
- (NSManagedObject *)_findObjectForRelationship:(NSRelationshipDescription *)relationshipInfo withData:(id)singleRelatedObjectData
{
- if ([singleRelatedObjectData isKindOfClass:[NSManagedObject class]]) {
- if (!([[singleRelatedObjectData entity] isKindOfEntity:relationshipInfo.destinationEntity] || [[singleRelatedObjectData entity] isEqual:relationshipInfo.destinationEntity]))
- return nil;
- return singleRelatedObjectData;
- } else if ([singleRelatedObjectData isKindOfClass:[NSURL class]]) {
- NSManagedObjectID *objectID = [self.managedObjectContext.persistentStoreCoordinator managedObjectIDForURIRepresentation:singleRelatedObjectData];
- return [self.managedObjectContext existingObjectWithID:objectID error:nil];
- } else if ([singleRelatedObjectData isKindOfClass:[NSManagedObjectID class]]) {
- return [self.managedObjectContext existingObjectWithID:singleRelatedObjectData error:nil];
- }
-
- id relatedValue = nil;
-
- NSEntityDescription *destination = [relationshipInfo destinationEntity];
- NSString *destinationKey = [relationshipInfo.userInfo objectForKey:kMagicalRecordImportClassNameKey];
- NSString *destinationName = [singleRelatedObjectData objectForKey:destinationKey];
- if (destinationName) {
- NSEntityDescription *customDestination = [NSEntityDescription entityForName:destinationName inManagedObjectContext:[self managedObjectContext]];
- if ([customDestination isKindOfEntity:destination])
- destination = customDestination;
- }
-
- if ([singleRelatedObjectData isKindOfClass:[NSNumber class]] || [singleRelatedObjectData isKindOfClass:[NSString class]])
- relatedValue = singleRelatedObjectData;
- else if ([singleRelatedObjectData isKindOfClass:[NSDictionary class]]) {
- NSEntityDescription *destinationEntity = [relationshipInfo destinationEntity];
- NSString *primaryKeyName = [relationshipInfo.userInfo valueForKey:kMagicalRecordImportRelationshipPrimaryKey] ?: primaryKeyNameFromString(relationshipInfo.destinationEntity.name);
- NSAttributeDescription *primaryKeyAttribute = [destinationEntity.attributesByName valueForKey:primaryKeyName];
- NSString *lookupKey = [[primaryKeyAttribute userInfo] valueForKey:kMagicalRecordImportMapKey] ?: [primaryKeyAttribute name];
- relatedValue = [singleRelatedObjectData valueForKeyPath:lookupKey];
-
- }
-
- if (!relatedValue)
- return nil;
+ if ([singleRelatedObjectData isKindOfClass:[NSManagedObject class]]) {
+ if (!([[singleRelatedObjectData entity] isKindOfEntity:relationshipInfo.destinationEntity] || [[singleRelatedObjectData entity] isEqual:relationshipInfo.destinationEntity]))
+ return nil;
+ return singleRelatedObjectData;
+ } else if ([singleRelatedObjectData isKindOfClass:[NSURL class]]) {
+ NSManagedObjectID *objectID = [self.managedObjectContext.persistentStoreCoordinator managedObjectIDForURIRepresentation:singleRelatedObjectData];
+ return [self.managedObjectContext existingObjectWithID:objectID error:nil];
+ } else if ([singleRelatedObjectData isKindOfClass:[NSManagedObjectID class]]) {
+ return [self.managedObjectContext existingObjectWithID:singleRelatedObjectData error:nil];
+ }
+
+ id relatedValue = nil;
+
+ NSEntityDescription *destination = [relationshipInfo destinationEntity];
+ NSString *destinationKey = [relationshipInfo.userInfo objectForKey:kMagicalRecordImportClassNameKey];
+ NSString *destinationName = [singleRelatedObjectData objectForKey:destinationKey];
+ if (destinationName) {
+ NSEntityDescription *customDestination = [NSEntityDescription entityForName:destinationName inManagedObjectContext:[self managedObjectContext]];
+ if ([customDestination isKindOfEntity:destination])
+ destination = customDestination;
+ }
+
+ if ([singleRelatedObjectData isKindOfClass:[NSNumber class]] || [singleRelatedObjectData isKindOfClass:[NSString class]])
+ relatedValue = singleRelatedObjectData;
+ else if ([singleRelatedObjectData isKindOfClass:[NSDictionary class]]) {
+ NSEntityDescription *destinationEntity = [relationshipInfo destinationEntity];
+ NSString *primaryKeyName = [relationshipInfo.userInfo valueForKey:kMagicalRecordImportRelationshipPrimaryKey] ?: primaryKeyNameFromString(relationshipInfo.destinationEntity.name);
+ NSAttributeDescription *primaryKeyAttribute = [destinationEntity.attributesByName valueForKey:primaryKeyName];
+ NSString *lookupKey = [[primaryKeyAttribute userInfo] valueForKey:kMagicalRecordImportMapKey] ?: [primaryKeyAttribute name];
+ relatedValue = [singleRelatedObjectData valueForKeyPath:lookupKey];
+
+ }
+
+ if (!relatedValue)
+ return nil;
- Class managedObjectClass = NSClassFromString([destination managedObjectClassName]);
- NSString *primaryKeyName = [relationshipInfo.userInfo valueForKey:kMagicalRecordImportRelationshipPrimaryKey] ?: primaryKeyNameFromString(relationshipInfo.destinationEntity.name);
- id object = [managedObjectClass findFirstWhere:primaryKeyName isEqualTo:relatedValue inContext:self.managedObjectContext];
- if ([singleRelatedObjectData isKindOfClass:[NSDictionary class]])
- [object updateValuesFromDictionary:singleRelatedObjectData];
- return object;
+ Class managedObjectClass = NSClassFromString([destination managedObjectClassName]);
+ NSString *primaryKeyName = [relationshipInfo.userInfo valueForKey:kMagicalRecordImportRelationshipPrimaryKey] ?: primaryKeyNameFromString(relationshipInfo.destinationEntity.name);
+ id object = [managedObjectClass findFirstWhere:primaryKeyName isEqualTo:relatedValue inContext:self.managedObjectContext];
+ if ([singleRelatedObjectData isKindOfClass:[NSDictionary class]])
+ [object updateValuesFromDictionary:singleRelatedObjectData];
+ return object;
}
- (void)_setAttributes:(NSDictionary *)attributes forDictionary:(NSDictionary *)objectData {
- [attributes enumerateKeysAndObjectsUsingBlock:^(NSString *attributeName, NSAttributeDescription *attributeInfo, BOOL *stop) {
- NSString *key = [attributeInfo.userInfo valueForKey:kMagicalRecordImportMapKey] ?: attributeInfo.name;
- if (!key.length)
- return;
-
- id value = [objectData valueForKeyPath:key];
-
- NSAttributeType attributeType = [attributeInfo attributeType];
- NSString *desiredAttributeType = [[attributeInfo userInfo] valueForKey:kMagicalRecordImportClassNameKey];
-
- if (desiredAttributeType && [desiredAttributeType hasSuffix:@"Color"]) {
- value = MRColorFromString(value);
- } else if (!desiredAttributeType && attributeType == NSDateAttributeType) {
- if (![value isKindOfClass:[NSDate class]])
- {
- NSString *dateFormat = [[attributeInfo userInfo] valueForKey:kMagicalRecordImportCustomDateFormat];
- value = MRDateFromString(value, dateFormat ?: kMagicalRecordImportDefaultDateFormat);
- }
- value = MRDateAdjustForDST(value);
- }
-
- value = value != [NSNull null] ? value : nil;
- [self setValue:value forKey:attributeName];
- }];
+ [attributes enumerateKeysAndObjectsUsingBlock:^(NSString *attributeName, NSAttributeDescription *attributeInfo, BOOL *stop) {
+ NSString *key = [attributeInfo.userInfo valueForKey:kMagicalRecordImportMapKey] ?: attributeInfo.name;
+ if (!key.length)
+ return;
+
+ id value = [objectData valueForKeyPath:key];
+
+ NSAttributeType attributeType = [attributeInfo attributeType];
+ NSString *desiredAttributeType = [[attributeInfo userInfo] valueForKey:kMagicalRecordImportClassNameKey];
+
+ if (desiredAttributeType && [desiredAttributeType hasSuffix:@"Color"]) {
+ value = MRColorFromString(value);
+ } else if (!desiredAttributeType && attributeType == NSDateAttributeType) {
+ if (![value isKindOfClass:[NSDate class]])
+ {
+ NSString *dateFormat = [[attributeInfo userInfo] valueForKey:kMagicalRecordImportCustomDateFormat];
+ value = MRDateFromString(value, dateFormat ?: kMagicalRecordImportDefaultDateFormat);
+ }
+ value = MRDateAdjustForDST(value);
+ }
+
+ value = value != [NSNull null] ? value : nil;
+ [self setValue:value forKey:attributeName];
+ }];
}
- (void)_addObject:(NSManagedObject *)relatedObject forRelationship:(NSRelationshipDescription *)relationshipInfo {
- NSAssert2(relatedObject, @"Cannot add nil to %@ for attribute %@", NSStringFromClass([self class]), relationshipInfo.name);
- NSAssert2([[relatedObject entity] isKindOfEntity:relationshipInfo.destinationEntity], @"related object entity %@ not same as destination entity %@", [[relatedObject entity] name], [[relationshipInfo destinationEntity] name]);
-
- //add related object to set
- SEL selector = NSSelectorFromString([NSString stringWithFormat:@"add%@Object:", attributeNameFromString([relationshipInfo name])]);
-
- @try
- {
+ NSAssert2(relatedObject, @"Cannot add nil to %@ for attribute %@", NSStringFromClass([self class]), relationshipInfo.name);
+ NSAssert2([[relatedObject entity] isKindOfEntity:relationshipInfo.destinationEntity], @"related object entity %@ not same as destination entity %@", [[relatedObject entity] name], [[relationshipInfo destinationEntity] name]);
+
+ //add related object to set
+ SEL selector = NSSelectorFromString([NSString stringWithFormat:@"add%@Object:", attributeNameFromString([relationshipInfo name])]);
+
+ @try
+ {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
- [self performSelector:selector withObject:relatedObject];
+ [self performSelector:selector withObject:relatedObject];
#pragma clank diagnostic pop
- }
- @catch (NSException *exception)
- {
- ARLog(@"Adding object for relationship failed: %@\n", relationshipInfo);
- ARLog(@"relatedObject.entity %@", [relatedObject entity]);
- ARLog(@"relationshipInfo.destinationEntity %@", [relationshipInfo destinationEntity]);
- ARLog(@"perform selector error: %@", exception);
- }
+ }
+ @catch (NSException *exception)
+ {
+ ARLog(@"Adding object for relationship failed: %@\n", relationshipInfo);
+ ARLog(@"relatedObject.entity %@", [relatedObject entity]);
+ ARLog(@"relationshipInfo.destinationEntity %@", [relationshipInfo destinationEntity]);
+ ARLog(@"perform selector error: %@", exception);
+ }
}
- (void)_setRelationships:(NSDictionary *)relationships forDictionary:(NSDictionary *)relationshipData withBlock:(NSManagedObject *(^)(NSRelationshipDescription *,id))setRelationship
{
- [relationships enumerateKeysAndObjectsUsingBlock:^(NSString *relationshipName, NSRelationshipDescription *relationshipInfo, BOOL *stop) {
- NSString *lookupKey = [relationshipInfo.userInfo valueForKey:kMagicalRecordImportMapKey] ?: relationshipName;
-
- id relatedObjectData = [relationshipData valueForKey:lookupKey];
- if (!relatedObjectData || [relatedObjectData isEqual:[NSNull null]])
- return;
-
- if (relationshipInfo.isToMany) {
- for (id singleRelatedObjectData in relatedObjectData) {
- NSManagedObject *obj = setRelationship(relationshipInfo, singleRelatedObjectData);
- [self _addObject:obj forRelationship:relationshipInfo];
- }
- } else {
- NSManagedObject *obj = setRelationship(relationshipInfo, relatedObjectData);
- [self _addObject:obj forRelationship:relationshipInfo];
- }
- }];
+ [relationships enumerateKeysAndObjectsUsingBlock:^(NSString *relationshipName, NSRelationshipDescription *relationshipInfo, BOOL *stop) {
+ NSString *lookupKey = [relationshipInfo.userInfo valueForKey:kMagicalRecordImportMapKey] ?: relationshipName;
+
+ id relatedObjectData = [relationshipData valueForKey:lookupKey];
+ if (!relatedObjectData || [relatedObjectData isEqual:[NSNull null]])
+ return;
+
+ if (relationshipInfo.isToMany) {
+ for (id singleRelatedObjectData in relatedObjectData) {
+ NSManagedObject *obj = setRelationship(relationshipInfo, singleRelatedObjectData);
+ [self _addObject:obj forRelationship:relationshipInfo];
+ }
+ } else {
+ NSManagedObject *obj = setRelationship(relationshipInfo, relatedObjectData);
+ [self _addObject:obj forRelationship:relationshipInfo];
+ }
+ }];
}
- (void) importValuesFromDictionary:(NSDictionary *)objectData
{
- @autoreleasepool {
- [self _setAttributes:self.entity.attributesByName forDictionary:objectData];
- NSManagedObjectContext *context = self.managedObjectContext;
- __block id safeSelf = self;
- [self _setRelationships:self.entity.relationshipsByName forDictionary:objectData withBlock:^NSManagedObject *(NSRelationshipDescription *relationshipInfo, id objectData) {
- NSManagedObject *relatedObject = nil;
-
- if ([objectData isKindOfClass:[NSDictionary class]]) {
- NSEntityDescription *destination = [relationshipInfo destinationEntity];
- NSString *destinationKey = [relationshipInfo.userInfo objectForKey:kMagicalRecordImportClassNameKey];
- NSString *destinationName = [objectData objectForKey:destinationKey];
- if (destinationName) {
- NSEntityDescription *customDestination = [NSEntityDescription entityForName:destinationName inManagedObjectContext:context];
- if ([customDestination isKindOfEntity:destination])
- destination = customDestination;
- }
- relatedObject = [safeSelf _createInstanceForEntity:destination withDictionary:objectData];
- } else {
- relatedObject = [safeSelf _findObjectForRelationship:relationshipInfo withData:objectData];
- }
- return relatedObject;
- }];
- }
+ @autoreleasepool {
+ [self _setAttributes:self.entity.attributesByName forDictionary:objectData];
+ NSManagedObjectContext *context = self.managedObjectContext;
+ __block id safeSelf = self;
+ [self _setRelationships:self.entity.relationshipsByName forDictionary:objectData withBlock:^NSManagedObject *(NSRelationshipDescription *relationshipInfo, id objectData) {
+ NSManagedObject *relatedObject = nil;
+
+ if ([objectData isKindOfClass:[NSDictionary class]]) {
+ NSEntityDescription *destination = [relationshipInfo destinationEntity];
+ NSString *destinationKey = [relationshipInfo.userInfo objectForKey:kMagicalRecordImportClassNameKey];
+ NSString *destinationName = [objectData objectForKey:destinationKey];
+ if (destinationName) {
+ NSEntityDescription *customDestination = [NSEntityDescription entityForName:destinationName inManagedObjectContext:context];
+ if ([customDestination isKindOfEntity:destination])
+ destination = customDestination;
+ }
+ relatedObject = [safeSelf _createInstanceForEntity:destination withDictionary:objectData];
+ } else {
+ relatedObject = [safeSelf _findObjectForRelationship:relationshipInfo withData:objectData];
+ }
+ return relatedObject;
+ }];
+ }
}
- (void)updateValuesFromDictionary:(NSDictionary *)objectData
{
- @autoreleasepool {
- [self _setAttributes:self.entity.attributesByName forDictionary:objectData];
- __block NSManagedObject *safeSelf = self;
- [self _setRelationships:self.entity.relationshipsByName forDictionary:objectData withBlock:^NSManagedObject *(NSRelationshipDescription *relationshipInfo, id objectData){
- NSManagedObject *relatedObject = [safeSelf _findObjectForRelationship:relationshipInfo withData:objectData];
-
- if (relatedObject) {
- if ([objectData isKindOfClass:[NSDictionary class]])
- [relatedObject importValuesFromDictionary:objectData];
- return relatedObject;
- }
+ @autoreleasepool {
+ [self _setAttributes:self.entity.attributesByName forDictionary:objectData];
+ __block NSManagedObject *safeSelf = self;
+ [self _setRelationships:self.entity.relationshipsByName forDictionary:objectData withBlock:^NSManagedObject *(NSRelationshipDescription *relationshipInfo, id objectData){
+ NSManagedObject *relatedObject = [safeSelf _findObjectForRelationship:relationshipInfo withData:objectData];
+
+ if (relatedObject) {
+ if ([objectData isKindOfClass:[NSDictionary class]])
+ [relatedObject importValuesFromDictionary:objectData];
+ return relatedObject;
+ }
- NSEntityDescription *destination = [relationshipInfo destinationEntity];
- NSString *destinationKey = [relationshipInfo.userInfo objectForKey:kMagicalRecordImportClassNameKey];
- NSString *destinationName = [objectData objectForKey:destinationKey];
- if (destinationName) {
- NSEntityDescription *customDestination = [NSEntityDescription entityForName:destinationName inManagedObjectContext:safeSelf.managedObjectContext];
- if ([customDestination isKindOfEntity:destination])
- destination = customDestination;
- }
- return [safeSelf _createInstanceForEntity:destination withDictionary:objectData];
- }];
- }
+ NSEntityDescription *destination = [relationshipInfo destinationEntity];
+ NSString *destinationKey = [relationshipInfo.userInfo objectForKey:kMagicalRecordImportClassNameKey];
+ NSString *destinationName = [objectData objectForKey:destinationKey];
+ if (destinationName) {
+ NSEntityDescription *customDestination = [NSEntityDescription entityForName:destinationName inManagedObjectContext:safeSelf.managedObjectContext];
+ if ([customDestination isKindOfEntity:destination])
+ destination = customDestination;
+ }
+ return [safeSelf _createInstanceForEntity:destination withDictionary:objectData];
+ }];
+ }
}
+ (id)importFromDictionary:(NSDictionary *)objectData {
- return [self importFromDictionary:objectData inContext:[NSManagedObjectContext defaultContext]];
+ return [self importFromDictionary:objectData inContext:[NSManagedObjectContext defaultContext]];
}
+ (id)importFromDictionary:(NSDictionary *)objectData inContext:(NSManagedObjectContext *)context {
- NSManagedObject *managedObject = [self createInContext:context];
- [managedObject importValuesFromDictionary:objectData];
- return managedObject;
+ NSManagedObject *managedObject = [self createInContext:context];
+ [managedObject importValuesFromDictionary:objectData];
+ return managedObject;
}
+ (id)updateFromDictionary:(NSDictionary *)objectData {
- return [self updateFromDictionary:objectData inContext:[NSManagedObjectContext defaultContext]];
+ return [self updateFromDictionary:objectData inContext:[NSManagedObjectContext defaultContext]];
}
+ (id)updateFromDictionary:(NSDictionary *)objectData inContext:(NSManagedObjectContext *)context
{
- NSEntityDescription *entity = [self entityDescription];
- NSString *attributeKey = [entity.userInfo valueForKey:kMagicalRecordImportPrimaryAttributeKey] ?: primaryKeyNameFromString(entity.name);
- NSAttributeDescription *primaryAttribute = [entity.attributesByName valueForKey:attributeKey];
- NSAssert3(primaryAttribute, @"Unable to determine primary attribute for %@. Specify either an attribute named %@ or the primary key in userInfo named '%@'", entity.name, attributeKey, kMagicalRecordImportPrimaryAttributeKey);
-
- NSString *lookupKey = [primaryAttribute.userInfo valueForKey:kMagicalRecordImportMapKey] ?: primaryAttribute.name;
- id value = [objectData valueForKey:lookupKey];
-
- NSManagedObject *managedObject = [self findFirstWhere:lookupKey isEqualTo:value inContext:context];
- if (!managedObject) {
- managedObject = [self createInContext:context];
- }
- [managedObject updateValuesFromDictionary:objectData];
-
- return managedObject;
+ NSEntityDescription *entity = [self entityDescription];
+ NSString *attributeKey = [entity.userInfo valueForKey:kMagicalRecordImportPrimaryAttributeKey] ?: primaryKeyNameFromString(entity.name);
+ NSAttributeDescription *primaryAttribute = [entity.attributesByName valueForKey:attributeKey];
+ NSAssert3(primaryAttribute, @"Unable to determine primary attribute for %@. Specify either an attribute named %@ or the primary key in userInfo named '%@'", entity.name, attributeKey, kMagicalRecordImportPrimaryAttributeKey);
+
+ NSString *lookupKey = [primaryAttribute.userInfo valueForKey:kMagicalRecordImportMapKey] ?: primaryAttribute.name;
+ id value = [objectData valueForKey:lookupKey];
+
+ NSManagedObject *managedObject = [self findFirstWhere:lookupKey isEqualTo:value inContext:context];
+ if (!managedObject) {
+ managedObject = [self createInContext:context];
+ }
+ [managedObject updateValuesFromDictionary:objectData];
+
+ return managedObject;
}
+ (NSArray *)importFromArray:(NSArray *)listOfObjectData
{
- return [self importFromArray:listOfObjectData inContext:[NSManagedObjectContext defaultContext]];
+ return [self importFromArray:listOfObjectData inContext:[NSManagedObjectContext defaultContext]];
}
+ (NSArray *)importFromArray:(NSArray *)listOfObjectData inContext:(NSManagedObjectContext *)context
{
- __block NSArray *objectIDs = nil;
-
- [MRCoreDataAction saveDataWithBlock:^(NSManagedObjectContext *localContext) {
- NSMutableArray *objects = [NSMutableArray array];
-
- for (NSDictionary *objectData in listOfObjectData) {
- [objects addObject:[self importFromDictionary:objectData inContext:localContext]];
- }
-
- if ([context obtainPermanentIDsForObjects:objects error:nil])
- objectIDs = [objects valueForKey:@"objectID"];
- }];
-
- return [self findAllWithPredicate:[NSPredicate predicateWithFormat:@"self IN %@", objectIDs] inContext:context];
+ __block NSArray *objectIDs = nil;
+
+ [MRCoreDataAction saveDataWithBlock:^(NSManagedObjectContext *localContext) {
+ NSMutableArray *objects = [NSMutableArray array];
+
+ for (NSDictionary *objectData in listOfObjectData) {
+ [objects addObject:[self importFromDictionary:objectData inContext:localContext]];
+ }
+
+ if ([context obtainPermanentIDsForObjects:objects error:nil])
+ objectIDs = [objects valueForKey:@"objectID"];
+ }];
+
+ return [self findAllWithPredicate:[NSPredicate predicateWithFormat:@"self IN %@", objectIDs] inContext:context];
}
+ (NSArray *)updateFromArray:(NSArray *)listOfObjectData {
- return [self updateFromArray:listOfObjectData inContext:[NSManagedObjectContext defaultContext]];
+ return [self updateFromArray:listOfObjectData inContext:[NSManagedObjectContext defaultContext]];
}
+ (NSArray *)updateFromArray:(NSArray *)listOfObjectData inContext:(NSManagedObjectContext *)context {
- __block NSArray *objectIDs = nil;
-
- [MRCoreDataAction saveDataWithBlock:^(NSManagedObjectContext *localContext) {
- NSMutableArray *objects = [NSMutableArray array];
-
- for (NSDictionary *objectData in listOfObjectData) {
- [objects addObject:[self updateFromDictionary:objectData inContext:localContext]];
- }
-
- if ([context obtainPermanentIDsForObjects:objects error:nil])
- objectIDs = [objects valueForKey:@"objectID"];
- }];
-
- return [self findAllWithPredicate:[NSPredicate predicateWithFormat:@"self IN %@", objectIDs] inContext:context];
+ __block NSArray *objectIDs = nil;
+
+ [MRCoreDataAction saveDataWithBlock:^(NSManagedObjectContext *localContext) {
+ NSMutableArray *objects = [NSMutableArray array];
+
+ for (NSDictionary *objectData in listOfObjectData) {
+ [objects addObject:[self updateFromDictionary:objectData inContext:localContext]];
+ }
+
+ if ([context obtainPermanentIDsForObjects:objects error:nil])
+ objectIDs = [objects valueForKey:@"objectID"];
+ }];
+
+ return [self findAllWithPredicate:[NSPredicate predicateWithFormat:@"self IN %@", objectIDs] inContext:context];
}
@end
View
376 Source/Categories/NSManagedObject+MagicalRecord.m
@@ -21,17 +21,17 @@ @implementation NSManagedObject (MagicalRecord)
- (id) inContext:(NSManagedObjectContext*)context
{
- NSError *error = nil;
- NSManagedObject *inContext = [context existingObjectWithID:[self objectID] error:&error];
- [MagicalRecordHelpers handleErrors:error];
-
- return inContext;
+ NSError *error = nil;
+ NSManagedObject *inContext = [context existingObjectWithID:[self objectID] error:&error];
+ [MagicalRecordHelpers handleErrors:error];
+
+ return inContext;
}
- (id) inThreadContext
{
- NSManagedObject *weakSelf = self;
- return [weakSelf inContext:[NSManagedObjectContext contextForCurrentThread]];
+ NSManagedObject *weakSelf = self;
+ return [weakSelf inContext:[NSManagedObjectContext contextForCurrentThread]];
}
- (BOOL) deleteEntity
@@ -49,7 +49,7 @@ - (BOOL) deleteInContext:(NSManagedObjectContext *)context
- (id) objectWithMinValueFor:(NSString *)property inContext:(NSManagedObjectContext *)context
{
NSFetchRequest *request = [[self class] createFetchRequestInContext:context];
-
+
NSPredicate *searchFor = [NSPredicate predicateWithFormat:@"SELF = %@ AND %K = min(%@)", self, property, property];
[request setPredicate:searchFor];
@@ -86,7 +86,7 @@ + (NSFetchRequest *)createFetchRequest
+ (NSFetchRequest *)createFetchRequestInContext:(NSManagedObjectContext *)context
{
NSFetchRequest *request = [NSFetchRequest new];
- request.entity = [self entityDescriptionInContext:context];
+ request.entity = [self entityDescriptionInContext:context];
return request;
}
@@ -124,13 +124,13 @@ + (id) executeFetchRequestAndReturnFirstObject:(NSFetchRequest *)request inConte
+ (NSArray *) sortAscending:(BOOL)ascending attributes:(NSArray *)attributesToSortBy
{
NSMutableArray *attributes = [NSMutableArray array];
-
- for (NSString *attributeName in attributesToSortBy)
- {
- NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:attributeName ascending:ascending];
- [attributes addObject:sortDescriptor];
- }
-
+
+ for (NSString *attributeName in attributesToSortBy)
+ {
+ NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:attributeName ascending:ascending];
+ [attributes addObject:sortDescriptor];
+ }
+
return attributes;
}
@@ -153,16 +153,16 @@ + (NSEntityDescription *)entityDescription
+ (NSEntityDescription *)entityDescriptionInContext:(NSManagedObjectContext *)context
{
- if ([self respondsToSelector:@selector(entityInManagedObjectContext:)])
- {
- NSEntityDescription *entity = [self performSelector:@selector(entityInManagedObjectContext:) withObject:context];
- return entity;
- }
- else
- {
- NSString *entityName = NSStringFromClass([self class]);
- return [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
- }
+ if ([self respondsToSelector:@selector(entityInManagedObjectContext:)])
+ {
+ NSEntityDescription *entity = [self performSelector:@selector(entityInManagedObjectContext:) withObject:context];
+ return entity;
+ }
+ else
+ {
+ NSString *entityName = NSStringFromClass([self class]);
+ return [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
+ }
}
+ (NSArray *)propertiesNamed:(NSArray *)properties
@@ -195,61 +195,61 @@ + (NSArray *)propertiesNamed:(NSArray *)properties
+ (id)createEntity
{
NSManagedObject *newEntity = [self createInContext:[NSManagedObjectContext contextForCurrentThread]];
-
+
return newEntity;
}
+ (id) createInContext:(NSManagedObjectContext *)context
{
- if ([self respondsToSelector:@selector(insertInManagedObjectContext:)])
- {
- id entity = [self performSelector:@selector(insertInManagedObjectContext:) withObject:context];
- return entity;
- }
- else
- {
- NSString *entityName = NSStringFromClass([self class]);
- return [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
- }
+ if ([self respondsToSelector:@selector(insertInManagedObjectContext:)])
+ {
+ id entity = [self performSelector:@selector(insertInManagedObjectContext:) withObject:context];
+ return entity;
+ }
+ else
+ {
+ NSString *entityName = NSStringFromClass([self class]);
+ return [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
+ }
}
#pragma mark - Entity deletion
+ (BOOL) deleteAllMatchingPredicate:(NSPredicate *)predicate
{
- return [self deleteAllMatchingPredicate:predicate inContext:[NSManagedObjectContext defaultContext]];
+ return [self deleteAllMatchingPredicate:predicate inContext:[NSManagedObjectContext defaultContext]];
}
+ (BOOL) deleteAllMatchingPredicate:(NSPredicate *)predicate inContext:(NSManagedObjectContext *)context
{
- NSFetchRequest *request = [self requestAllWithPredicate:predicate inContext:context];
+ NSFetchRequest *request = [self requestAllWithPredicate:predicate inContext:context];
[request setIncludesSubentities:NO];
[request setIncludesPropertyValues:NO];
-
+
NSArray *objectsToTruncate = [self executeFetchRequest:request inContext:context];
-
+
for (id objectToTruncate in objectsToTruncate)
- {
+ {
[objectToTruncate deleteInContext:context];
}
-
+
return YES;
}
+ (BOOL) truncateAll
{
- [self truncateAllInContext:[NSManagedObjectContext contextForCurrentThread]];
- return YES;
+ [self truncateAllInContext:[NSManagedObjectContext contextForCurrentThread]];
+ return YES;
}
+ (BOOL) truncateAllInContext:(NSManagedObjectContext *)context
{
- NSArray *allEntities = [self findAllInContext:context];
- for (NSManagedObject *obj in allEntities)
- {
- [obj deleteInContext:context];
- }
- return YES;
+ NSArray *allEntities = [self findAllInContext:context];
+ for (NSManagedObject *obj in allEntities)
+ {
+ [obj deleteInContext:context];
+ }
+ return YES;
}
#pragma mark - Counting entities
@@ -272,13 +272,13 @@ + (NSNumber *) numberOfEntitiesWithPredicate:(NSPredicate *)searchTerm;
+ (NSNumber *) numberOfEntitiesWithPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context
{
-
+
return [NSNumber numberWithUnsignedInteger:[self countOfEntitiesWithPredicate:searchTerm inContext:context]];
}
+ (NSUInteger) countOfEntities;
{
- return [self countOfEntitiesWithContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self countOfEntitiesWithContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSUInteger) countOfEntitiesWithContext:(NSManagedObjectContext *)context;
@@ -287,12 +287,12 @@ + (NSUInteger) countOfEntitiesWithContext:(NSManagedObjectContext *)context;
NSUInteger count = [context countForFetchRequest:[self createFetchRequestInContext:context] error:&error];
[MagicalRecordHelpers handleErrors:error];
- return count;
+ return count;
}
+ (NSUInteger) countOfEntitiesWithPredicate:(NSPredicate *)searchFilter;
{
- return [self countOfEntitiesWithPredicate:searchFilter inContext:[NSManagedObjectContext defaultContext]];
+ return [self countOfEntitiesWithPredicate:searchFilter inContext:[NSManagedObjectContext defaultContext]];
}
+ (NSUInteger) countOfEntitiesWithPredicate:(NSPredicate *)searchFilter inContext:(NSManagedObjectContext *)context;
@@ -303,72 +303,72 @@ + (NSUInteger) countOfEntitiesWithPredicate:(NSPredicate *)searchFilter inContex
NSUInteger count = [context countForFetchRequest:request error:&error];
[MagicalRecordHelpers handleErrors:error];
-
- return count;
+
+ return count;
}
+ (BOOL) hasAtLeastOneEntity
{
- return [self hasAtLeastOneEntityInContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self hasAtLeastOneEntityInContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (BOOL) hasAtLeastOneEntityInContext:(NSManagedObjectContext *)context
{
- return [[self numberOfEntitiesWithContext:context] intValue] > 0;
+ return [[self numberOfEntitiesWithContext:context] intValue] > 0;
}
#pragma mark - Fetch requests for groups
+ (NSFetchRequest *) requestAll
{
- return [self createFetchRequestInContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self createFetchRequestInContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSFetchRequest *) requestAllInContext:(NSManagedObjectContext *)context
{
- return [self createFetchRequestInContext:context];
+ return [self createFetchRequestInContext:context];
}
+ (NSFetchRequest *) requestAllWithPredicate:(NSPredicate *)searchTerm
{
- return [self requestAllWithPredicate:searchTerm inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self requestAllWithPredicate:searchTerm inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSFetchRequest *) requestAllWithPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context
{
- NSFetchRequest *request = [self createFetchRequestInContext:context];
- [request setPredicate:searchTerm];
- return request;
+ NSFetchRequest *request = [self createFetchRequestInContext:context];
+ [request setPredicate:searchTerm];
+ return request;
}
+ (NSFetchRequest *) requestAllWhere:(NSString *)property isEqualTo:(id)value
{
- return [self requestAllWhere:property isEqualTo:value inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self requestAllWhere:property isEqualTo:value inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSFetchRequest *) requestAllWhere:(NSString *)property isEqualTo:(id)value inContext:(NSManagedObjectContext *)context
{
- return [self requestAllWithPredicate:[NSPredicate predicateWithFormat:@"%K = %@", property, value] inContext:context];
+ return [self requestAllWithPredicate:[NSPredicate predicateWithFormat:@"%K = %@", property, value] inContext:context];
}
+ (NSFetchRequest *) requestAllWhere:(NSString *)property isEqualTo:(id)value sortedBy:(NSString *)sortTerm ascending:(BOOL)ascending
{
- return [self requestAllWhere:property isEqualTo:value sortedBy:sortTerm ascending:ascending inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self requestAllWhere:property isEqualTo:value sortedBy:sortTerm ascending:ascending inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSFetchRequest *) requestAllWhere:(NSString *)property isEqualTo:(id)value sortedBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context
{
- return [self requestAllSortedBy:sortTerm ascending:ascending withPredicate:[NSPredicate predicateWithFormat:@"%K = %@", property, value] inContext:context];
+ return [self requestAllSortedBy:sortTerm ascending:ascending withPredicate:[NSPredicate predicateWithFormat:@"%K = %@", property, value] inContext:context];
}
+ (NSFetchRequest *) requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending
{
- return [self requestAllSortedBy:sortTerm ascending:ascending withPredicate:nil];
+ return [self requestAllSortedBy:sortTerm ascending:ascending withPredicate:nil];
}
+ (NSFetchRequest *) requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context
{
- return [self requestAllSortedBy:sortTerm ascending:ascending withPredicate:nil inContext:context];
+ return [self requestAllSortedBy:sortTerm ascending:ascending withPredicate:nil inContext:context];
}
+ (NSFetchRequest *) requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm
@@ -378,11 +378,11 @@ + (NSFetchRequest *) requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)asc
+ (NSFetchRequest *) requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context
{
- NSFetchRequest *request = [self createFetchRequestInContext:context];
+ NSFetchRequest *request = [self createFetchRequestInContext:context];
[request setPredicate:searchTerm];
[request setIncludesSubentities:NO];
[request setFetchBatchSize:[self defaultBatchSize]];
- NSSortDescriptor *sortBy = [NSSortDescriptor sortDescriptorWithKey:sortTerm ascending:ascending];
+ NSSortDescriptor *sortBy = [NSSortDescriptor sortDescriptorWithKey:sortTerm ascending:ascending];
[request setSortDescriptors:[NSArray arrayWithObject:sortBy]];
return request;
}
@@ -390,289 +390,289 @@ + (NSFetchRequest *) requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)asc
#pragma mark - Fetch requests for single items
+ (NSFetchRequest *) requestFirst {
- return [self requestFirstInContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self requestFirstInContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSFetchRequest *) requestFirstInContext:(NSManagedObjectContext *)context {
- NSFetchRequest *request = [self requestAllInContext:context];
- request.fetchLimit = 1;
- return request;
+ NSFetchRequest *request = [self requestAllInContext:context];
+ request.fetchLimit = 1;
+ return request;
}
+ (NSFetchRequest *) requestFirstWithPredicate:(NSPredicate *)searchTerm {
- return [self requestFirstWithPredicate:searchTerm inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self requestFirstWithPredicate:searchTerm inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSFetchRequest *) requestFirstWithPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context {
- NSFetchRequest *request = [self requestAllWithPredicate:searchTerm inContext:context];
- request.fetchLimit = 1;
- return request;
+ NSFetchRequest *request = [self requestAllWithPredicate:searchTerm inContext:context];
+ request.fetchLimit = 1;
+ return request;
}
+ (NSFetchRequest *) requestFirstWhere:(NSString *)property isEqualTo:(id)searchValue {
- return [self requestFirstWhere:property isEqualTo:searchValue inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self requestFirstWhere:property isEqualTo:searchValue inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSFetchRequest *) requestFirstWhere:(NSString *)property isEqualTo:(id)searchValue inContext:(NSManagedObjectContext *)context {
- NSFetchRequest *request = [self requestAllWhere:property isEqualTo:searchValue inContext:context];
- request.fetchLimit = 1;
- return request;
+ NSFetchRequest *request = [self requestAllWhere:property isEqualTo:searchValue inContext:context];
+ request.fetchLimit = 1;
+ return request;
}
+ (NSFetchRequest *) requestFirstWhere:(NSString *)property isEqualTo:(id)searchValue sortedBy:(NSString *)sortTerm ascending:(BOOL)ascending {
- return [self requestFirstWhere:property isEqualTo:searchValue sortedBy:sortTerm ascending:ascending inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self requestFirstWhere:property isEqualTo:searchValue sortedBy:sortTerm ascending:ascending inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSFetchRequest *) requestFirstWhere:(NSString *)property isEqualTo:(id)searchValue sortedBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context {
- NSFetchRequest *request = [self requestAllWhere:property isEqualTo:searchValue sortedBy:sortTerm ascending:ascending inContext:context];
- request.fetchLimit = 1;
- return request;
+ NSFetchRequest *request = [self requestAllWhere:property isEqualTo:searchValue sortedBy:sortTerm ascending:ascending inContext:context];
+ request.fetchLimit = 1;
+ return request;
}
+ (NSFetchRequest *) requestFirstSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending {
- return [self requestFirstSortedBy:sortTerm ascending:ascending inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self requestFirstSortedBy:sortTerm ascending:ascending inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSFetchRequest *) requestFirstSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context {
- NSFetchRequest *request = [self requestAllSortedBy:sortTerm ascending:ascending inContext:context];
- request.fetchLimit = 1;
- return request;
+ NSFetchRequest *request = [self requestAllSortedBy:sortTerm ascending:ascending inContext:context];
+ request.fetchLimit = 1;
+ return request;
}
+ (NSFetchRequest *) requestFirstSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm {
- return [self requestFirstSortedBy:sortTerm ascending:ascending withPredicate:searchTerm inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self requestFirstSortedBy:sortTerm ascending:ascending withPredicate:searchTerm inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSFetchRequest *) requestFirstSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context {
- NSFetchRequest *request = [self requestAllSortedBy:sortTerm ascending:ascending withPredicate:searchTerm inContext:context];
- request.fetchLimit = 1;
- return request;
+ NSFetchRequest *request = [self requestAllSortedBy:sortTerm ascending:ascending withPredicate:searchTerm inContext:context];
+ request.fetchLimit = 1;
+ return request;
}
#pragma mark - Fetch requests for single items (deprecated)
+ (NSFetchRequest *) requestFirstByAttribute:(NSString *)attribute withValue:(id)searchValue {
- return [self requestFirstWhere:attribute isEqualTo:searchValue];
+ return [self requestFirstWhere:attribute isEqualTo:searchValue];
}
+ (NSFetchRequest *) requestFirstByAttribute:(NSString *)attribute withValue:(id)searchValue inContext:(NSManagedObjectContext *)context {
- return [self requestFirstWhere:attribute isEqualTo:searchValue inContext:context];
+ return [self requestFirstWhere:attribute isEqualTo:searchValue inContext:context];
}
#pragma mark - Fetching groups
+ (NSArray *)findAll {
- return [self findAllInContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self findAllInContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSArray *)findAllInContext:(NSManagedObjectContext *)context {
- return [self executeFetchRequest:[self requestAllInContext:context] inContext:context];
+ return [self executeFetchRequest:[self requestAllInContext:context] inContext:context];
}
+ (NSArray *)findAllWithPredicate:(NSPredicate *)searchTerm {
- return [self findAllWithPredicate:searchTerm inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self findAllWithPredicate:searchTerm inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSArray *)findAllWithPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context {
- return [self executeFetchRequest:[self requestAllWithPredicate:searchTerm inContext:context] inContext:context];
+ return [self executeFetchRequest:[self requestAllWithPredicate:searchTerm inContext:context] inContext:context];
}
+ (NSArray *) findAllWhere:(NSString *)property isEqualTo:(id)value {
- return [self findAllWhere:property isEqualTo:value inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self findAllWhere:property isEqualTo:value inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSArray *) findAllWhere:(NSString *)property isEqualTo:(id)value inContext:(NSManagedObjectContext *)context {
- return [self executeFetchRequest:[self requestAllWhere:property isEqualTo:value inContext:context] inContext:context];
+ return [self executeFetchRequest:[self requestAllWhere:property isEqualTo:value inContext:context] inContext:context];
}
+ (NSArray *) findAllWhere:(NSString *)property isEqualTo:(id)value sortedBy:(NSString *)sortTerm ascending:(BOOL)ascending {
- return [self findAllWhere:property isEqualTo:value sortedBy:sortTerm ascending:ascending inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self findAllWhere:property isEqualTo:value sortedBy:sortTerm ascending:ascending inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSArray *) findAllWhere:(NSString *)property isEqualTo:(id)value sortedBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context {
- return [self executeFetchRequest:[self requestAllWhere:property isEqualTo:value sortedBy:sortTerm ascending:ascending inContext:context] inContext:context];
+ return [self executeFetchRequest:[self requestAllWhere:property isEqualTo:value sortedBy:sortTerm ascending:ascending inContext:context] inContext:context];
}
+ (NSArray *)findAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending {
- return [self findAllSortedBy:sortTerm ascending:ascending inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self findAllSortedBy:sortTerm ascending:ascending inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSArray *)findAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context {
- return [self executeFetchRequest:[self requestAllSortedBy:sortTerm ascending:ascending inContext:context] inContext:context];
+ return [self executeFetchRequest:[self requestAllSortedBy:sortTerm ascending:ascending inContext:context] inContext:context];
}
+ (NSArray *)findAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm {
- return [self findAllSortedBy:sortTerm ascending:ascending withPredicate:searchTerm inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self findAllSortedBy:sortTerm ascending:ascending withPredicate:searchTerm inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (NSArray *)findAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context {
- return [self executeFetchRequest:[self requestAllSortedBy:sortTerm ascending:ascending withPredicate:searchTerm inContext:context] inContext:context];
+ return [self executeFetchRequest:[self requestAllSortedBy:sortTerm ascending:ascending withPredicate:searchTerm inContext:context] inContext:context];
}
#pragma mark - Fetching object groups (deprecated)
+ (NSArray *)findByAttribute:(NSString *)attribute withValue:(id)searchValue {
- return [self findAllWhere:attribute isEqualTo:searchValue];
+ return [self findAllWhere:attribute isEqualTo:searchValue];
}
+ (NSArray *)findByAttribute:(NSString *)attribute withValue:(id)searchValue inContext:(NSManagedObjectContext *)context {
- return [self findAllWhere:attribute isEqualTo:searchValue inContext:context];
+ return [self findAllWhere:attribute isEqualTo:searchValue inContext:context];
}
+ (NSArray *)findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending {
- return [self findAllWhere:attribute isEqualTo:searchValue sortedBy:sortTerm ascending:ascending];
+ return [self findAllWhere:attribute isEqualTo:searchValue sortedBy:sortTerm ascending:ascending];
}
+ (NSArray *)findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context {
- return [self findAllWhere:attribute isEqualTo:searchValue sortedBy:sortTerm ascending:ascending inContext:context];
+ return [self findAllWhere:attribute isEqualTo:searchValue sortedBy:sortTerm ascending:ascending inContext:context];
}
#pragma mark - Fetching single objects
+ (id)findFirst {
- return [self findFirstInContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self findFirstInContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (id)findFirstInContext:(NSManagedObjectContext *)context {
- return [self executeFetchRequestAndReturnFirstObject:[self requestAll] inContext:context];
+ return [self executeFetchRequestAndReturnFirstObject:[self requestAll] inContext:context];
}
+ (id)findFirstWithPredicate:(NSPredicate *)searchTerm {
- return [self findFirstWithPredicate:searchTerm inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self findFirstWithPredicate:searchTerm inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (id)findFirstWithPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context {
- return [self executeFetchRequestAndReturnFirstObject:[self requestAllWithPredicate:searchTerm inContext:context]];
+ return [self executeFetchRequestAndReturnFirstObject:[self requestAllWithPredicate:searchTerm inContext:context]];
}
+ (id)findFirstWhere:(NSString *)property isEqualTo:(id)searchValue {
- return [self findFirstWhere:property isEqualTo:searchValue inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self findFirstWhere:property isEqualTo:searchValue inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (id)findFirstWhere:(NSString *)property isEqualTo:(id)searchValue inContext:(NSManagedObjectContext *)context {
- return [self executeFetchRequestAndReturnFirstObject:[self requestAllWhere:property isEqualTo:searchValue inContext:context] inContext:context];
+ return [self executeFetchRequestAndReturnFirstObject:[self requestAllWhere:property isEqualTo:searchValue inContext:context] inContext:context];
}
+ (id)findFirstWhere:(NSString *)property isEqualTo:(id)searchValue sortedBy:(NSString *)sortTerm ascending:(BOOL)ascending {
- return [self findFirstWhere:property isEqualTo:searchValue sortedBy:sortTerm ascending:ascending inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self findFirstWhere:property isEqualTo:searchValue sortedBy:sortTerm ascending:ascending inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (id)findFirstWhere:(NSString *)property isEqualTo:(id)searchValue sortedBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context {
- return [self executeFetchRequestAndReturnFirstObject:[self requestAllWhere:property isEqualTo:searchValue sortedBy:sortTerm ascending:ascending inContext:context] inContext:context];
+ return [self executeFetchRequestAndReturnFirstObject:[self requestAllWhere:property isEqualTo:searchValue sortedBy:sortTerm ascending:ascending inContext:context] inContext:context];
}
+ (id)findFirstSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending {
- return [self findFirstSortedBy:sortTerm ascending:ascending inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self findFirstSortedBy:sortTerm ascending:ascending inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (id)findFirstSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context {
- return [self executeFetchRequestAndReturnFirstObject:[self requestAllSortedBy:sortTerm ascending:ascending inContext:context] inContext:context];
+ return [self executeFetchRequestAndReturnFirstObject:[self requestAllSortedBy:sortTerm ascending:ascending inContext:context] inContext:context];
}
+ (id)findFirstSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm {
- return [self findFirstSortedBy:sortTerm ascending:ascending withPredicate:searchTerm inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self findFirstSortedBy:sortTerm ascending:ascending withPredicate:searchTerm inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (id)findFirstSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context {
- return [self executeFetchRequestAndReturnFirstObject:[self requestAllSortedBy:sortTerm ascending:ascending withPredicate:searchTerm inContext:context] inContext:context];
+ return [self executeFetchRequestAndReturnFirstObject:[self requestAllSortedBy:sortTerm ascending:ascending withPredicate:searchTerm inContext:context] inContext:context];
}
+ (id)findFirstWithPredicate:(NSPredicate *)searchTerm andRetrieveAttributes:(NSArray *)attributes {
- return [self findFirstWithPredicate:searchTerm andRetrieveAttributes:attributes inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self findFirstWithPredicate:searchTerm andRetrieveAttributes:attributes inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (id)findFirstWithPredicate:(NSPredicate *)searchTerm andRetrieveAttributes:(NSArray *)attributes inContext:(NSManagedObjectContext *)context {
- NSFetchRequest *request = [self requestFirstWithPredicate:searchTerm inContext:context];
- request.propertiesToFetch = attributes;
- return [self executeFetchRequestAndReturnFirstObject:request inContext:context];
+ NSFetchRequest *request = [self requestFirstWithPredicate:searchTerm inContext:context];
+ request.propertiesToFetch = attributes;
+ return [self executeFetchRequestAndReturnFirstObject:request inContext:context];
}
+ (id)findFirstSortedBy:(NSString *)sortBy ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm andRetrieveAttributes:(id)firstAttribute, ... {
- NSMutableArray *attribs = [NSMutableArray array];
- va_list args;
- id obj;
- if (firstAttribute)
- {
- [attribs addObject:firstAttribute];
- va_start(args, firstAttribute);
- while ((obj = va_arg(args, id)))
- [attribs addObject:obj];
- va_end(args);
- }
- return [self findFirstSortedBy:sortBy ascending:ascending withPredicate:searchTerm attributesToRetrieve:attribs];
+ NSMutableArray *attribs = [NSMutableArray array];
+ va_list args;
+ id obj;
+ if (firstAttribute)
+ {
+ [attribs addObject:firstAttribute];
+ va_start(args, firstAttribute);
+ while ((obj = va_arg(args, id)))
+ [attribs addObject:obj];
+ va_end(args);
+ }
+ return [self findFirstSortedBy:sortBy ascending:ascending withPredicate:searchTerm attributesToRetrieve:attribs];
}
+ (id)findFirstSortedBy:(NSString *)sortBy ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context andRetrieveAttributes:(id)firstAttribute, ... {
- NSMutableArray *attribs = [NSMutableArray array];
- va_list args;
- id obj;
- if (firstAttribute)
- {
- [attribs addObject:firstAttribute];
- va_start(args, firstAttribute);
- while ((obj = va_arg(args, id)))
- [attribs addObject:obj];
- va_end(args);
- }
- return [self findFirstSortedBy:sortBy ascending:ascending withPredicate:searchTerm attributesToRetrieve:attribs inContext:context];
+ NSMutableArray *attribs = [NSMutableArray array];
+ va_list args;
+ id obj;
+ if (firstAttribute)
+ {
+ [attribs addObject:firstAttribute];
+ va_start(args, firstAttribute);
+ while ((obj = va_arg(args, id)))
+ [attribs addObject:obj];
+ va_end(args);
+ }
+ return [self findFirstSortedBy:sortBy ascending:ascending withPredicate:searchTerm attributesToRetrieve:attribs inContext:context];
}
+ (id)findFirstSortedBy:(NSString *)sortBy ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm attributesToRetrieve:(NSArray *)attributes {
- return [self findFirstSortedBy:sortBy ascending:ascending withPredicate:searchTerm attributesToRetrieve:attributes inContext:[NSManagedObjectContext contextForCurrentThread]];
+ return [self findFirstSortedBy:sortBy ascending:ascending withPredicate:searchTerm attributesToRetrieve:attributes inContext:[NSManagedObjectContext contextForCurrentThread]];
}
+ (id)findFirstSortedBy:(NSString *)sortBy ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm attributesToRetrieve:(NSArray *)attributes inContext:(NSManagedObjectContext *)context {
- NSFetchRequest *request = [self requestFirstSortedBy:sortBy ascending:ascending withPredicate:searchTerm inContext:context];
- request.propertiesToFetch = attributes;
- return [self executeFetchRequestAndReturnFirstObject:request inContext:context];
+ NSFetchRequest *request = [self requestFirstSortedBy:sortBy ascending:ascending withPredicate:searchTerm inContext:context];
+ request.propertiesToFetch = attributes;
+ return [self executeFetchRequestAndReturnFirstObject:request inContext:context];
}
#pragma mark - Fetching single objects (deprecated)
+ (id)findFirstWithPredicate:(NSPredicate *)searchTerm sortedBy:(NSString *)sortBy ascending:(BOOL)ascending andRetrieveAttributes:(id)firstAttribute, ... {
- NSMutableArray *attribs = [NSMutableArray array];
- va_list args;
- id obj;
- if (firstAttribute)
- {
- [attribs addObject:firstAttribute];
- va_start(args, firstAttribute);
- while ((obj = va_arg(args, id)))
- [attribs addObject:obj];
- va_end(args);
- }
- return [self findFirstSortedBy:sortBy ascending:ascending withPredicate:searchTerm attributesToRetrieve:attribs];
+ NSMutableArray *attribs = [NSMutableArray array];
+ va_list args;
+ id obj;
+ if (firstAttribute)
+ {
+ [attribs addObject:firstAttribute];
+ va_start(args, firstAttribute);
+ while ((obj = va_arg(args, id)))
+ [attribs addObject:obj];
+ va_end(args);
+ }
+ return [self findFirstSortedBy:sortBy ascending:ascending withPredicate:searchTerm attributesToRetrieve:attribs];
}
+ (id)findFirstWithPredicate:(NSPredicate *)searchTerm sortedBy:(NSString *)sortBy ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context andRetrieveAttributes:(id)firstAttribute, ... {
- NSMutableArray *attribs = [NSMutableArray array];
- va_list args;
- id obj;
- if (firstAttribute)
- {
- [attribs addObject:firstAttribute];
- va_start(args, firstAttribute);
- while ((obj = va_arg(args, id)))
- [attribs addObject:obj];
- va_end(args);
- }
- return [self findFirstSortedBy:sortBy ascending:ascending withPredicate:searchTerm attributesToRetrieve:attribs inContext:context];
+ NSMutableArray *attribs = [NSMutableArray array];
+ va_list args;
+ id obj;
+ if (firstAttribute)
+ {
+ [attribs addObject:firstAttribute];
+ va_start(args, firstAttribute);
+ while ((obj = va_arg(args, id)))
+ [attribs addObject:obj];
+ va_end(args);
+ }
+ return [self findFirstSortedBy:sortBy ascending:ascending withPredicate:searchTerm attributesToRetrieve:attribs inContext:context];
}
+ (id)findFirstByAttribute:(NSString *)attribute withValue:(id)searchValue {
- return [self findFirstWhere:attribute isEqualTo:searchValue];
+ return [self findFirstWhere:attribute isEqualTo:searchValue];
}
+ (id)findFirstByAttribute:(NSString *)attribute withValue:(id)searchValue inContext:(NSManagedObjectContext *)context {
- return [self findFirstWhere:attribute isEqualTo:searchValue inContext:context];
+ return [self findFirstWhere:attribute isEqualTo:searchValue inContext:context];
}
+ (id)findFirstWithPredicate:(NSPredicate *)searchterm sortedBy:(NSString *)property ascending:(BOOL)ascending {
- return [self findFirstSortedBy:property ascending:ascending withPredicate:searchterm];
+ return [self findFirstSortedBy:property ascending:ascending withPredicate:searchterm];
}
+ (id)findFirstWithPredicate:(NSPredicate *)searchterm sortedBy:(NSString *)property ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context {
- return [self findFirstSortedBy:property ascending:ascending withPredicate:searchterm inContext:context];
+ return [self findFirstSortedBy:property ascending:ascending withPredicate:searchterm inContext:context];
}
#pragma mark - Fetched results controllers
@@ -720,7 +720,7 @@ + (NSFetchedResultsController *) fetchRequest:(NSFetchRequest *)request groupedB
{
NSString *cacheName = TARGET_OS_IPHONE ? [NSString stringWithFormat:@"MagicalRecord-Cache-%@", NSStringFromClass([self class])] : nil;
NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:group cacheName:cacheName];
- [self performFetch:controller];
+ [self performFetch:controller];
return controller;
}
View
124 Source/Categories/NSManagedObjectContext+MagicalRecord.m
@@ -24,24 +24,24 @@ @implementation NSManagedObjectContext (MagicalRecord)
+ (NSManagedObjectContext *)defaultContext
{
- return defaultManageObjectContext_;
+ return defaultManageObjectContext_;
}
+ (void) setDefaultContext:(NSManagedObjectContext *)moc
{
- @synchronized(self) {
- defaultManageObjectContext_ = moc;
- }
+ @synchronized(self) {
+ defaultManageObjectContext_ = moc;
+ }
}
+ (void) resetDefaultContext
{
- [[NSManagedObjectContext defaultContext] performSelectorOnMainThread:@selector(reset) withObject:nil waitUntilDone:NO];
+ [[NSManagedObjectContext defaultContext] performSelectorOnMainThread:@selector(reset) withObject:nil waitUntilDone:NO];
}
+ (void) resetContextForCurrentThread
{
- [[NSManagedObjectContext contextForCurrentThread] reset];
+ [[NSManagedObjectContext contextForCurrentThread] reset];
}
+ (NSManagedObjectContext *) contextForCurrentThread
@@ -49,14 +49,14 @@ + (NSManagedObjectContext *) contextForCurrentThread
if ([NSThread isMainThread])
return [self defaultContext];
- NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary];
- NSManagedObjectContext *threadContext = [threadDict objectForKey:kMagicalRecordManagedObjectContextKey];
- if (threadContext == nil)
- {
- threadContext = [self contextThatNotifiesDefaultContextOnMainThread];
- [threadDict setObject:threadContext forKey:kMagicalRecordManagedObjectContextKey];
- }
- return threadContext;
+ NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary];
+ NSManagedObjectContext *threadContext = [threadDict objectForKey:kMagicalRecordManagedObjectContextKey];
+ if (threadContext == nil)
+ {
+ threadContext = [self contextThatNotifiesDefaultContextOnMainThread];
+ [threadDict setObject:threadContext forKey:kMagicalRecordManagedObjectContextKey];
+ }
+ return threadContext;
}
@@ -70,7 +70,7 @@ - (void) observeContext:(NSManagedObjectContext *)otherContext
- (void) observeContextOnMainThread:(NSManagedObjectContext *)otherContext
{
- // ARLog(@"Start Observing on Main Thread");
+ // ARLog(@"Start Observing on Main Thread");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mergeChangesOnMainThread:)
name:NSManagedObjectContextDidSaveNotification
@@ -79,7 +79,7 @@ - (void) observeContextOnMainThread:(NSManagedObjectContext *)otherContext
- (void) stopObservingContext:(NSManagedObjectContext *)otherContext
{
- // ARLog(@"Stop Observing Context");
+ // ARLog(@"Stop Observing Context");
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSManagedObjectContextDidSaveNotification
object:otherContext];
@@ -88,9 +88,9 @@ - (void) stopObservingContext:(NSManagedObjectContext *)otherContext
- (void) mergeChangesFromNotification:(NSNotification *)notification
{
ARLog(@"Merging changes to %@context%@",
- self == [NSManagedObjectContext defaultContext] ? @"*** DEFAULT *** " : @"",
- ([NSThread isMainThread] ? @" *** on Main Thread ***" : @""));
-
+ self == [NSManagedObjectContext defaultContext] ? @"*** DEFAULT *** " : @"",
+ ([NSThread isMainThread] ? @" *** on Main Thread ***" : @""));
+
[self mergeChangesFromContextDidSaveNotification:notification];
}
@@ -119,9 +119,9 @@ - (BOOL) saveWithErrorHandler:(void(^)(NSError *))errorCallback
@try
{
ARLog(@"Saving %@Context%@",
- self == [[self class] defaultContext] ? @" *** Default *** ": @"",
- ([NSThread isMainThread] ? @" *** on Main Thread ***" : @""));
-
+ self == [[self class] defaultContext] ? @" *** Default *** ": @"",
+ ([NSThread isMainThread] ? @" *** on Main Thread ***" : @""));
+
saved = [self save:&error];
}
@catch (NSException *exception)
@@ -129,27 +129,27 @@ - (BOOL) saveWithErrorHandler:(void(^)(NSError *))errorCallback
ARLog(@"Problem saving: %@", (id)[exception userInfo] ?: (id)[exception reason]);
}
@finally
- {
- if (!saved)
- {
- if (errorCallback)
- {
- errorCallback(error);
- }
- else if (error)
- {
- [MagicalRecordHelpers handleErrors:error];
- }
- }
- }
+ {
+ if (!saved)
+ {
+ if (errorCallback)
+ {
+ errorCallback(error);
+ }
+ else if (error)
+ {
+ [MagicalRecordHelpers handleErrors:error];
+ }
+ }
+ }
return saved && error == nil;
}
- (void) saveWrapper
{
- @autoreleasepool {
- [self save];
- }
+ @autoreleasepool {
+ [self save];
+ }
}
- (BOOL) saveOnBackgroundThread
@@ -168,39 +168,39 @@ - (BOOL) saveOnMainThread
- (BOOL) notifiesMainContextOnSave
{
- NSNumber *notifies = objc_getAssociatedObject(self, kNotfiesMainContextKey);
- return [notifies boolValue];
+ NSNumber *notifies = objc_getAssociatedObject(self, kNotfiesMainContextKey);
+ return [notifies boolValue];
}
- (void) setNotifiesMainContextOnSave:(BOOL)enabled
{
- NSManagedObjectContext *mainContext = [[self class] defaultContext];
- if (self != mainContext)
- {
- objc_setAssociatedObject(self, kNotfiesMainContextKey, [NSNumber numberWithBool:enabled], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- if (enabled)
- [mainContext observeContextOnMainThread:self];
- else
- [mainContext stopObservingContext:self];
- }
+ NSManagedObjectContext *mainContext = [[self class] defaultContext];
+ if (self != mainContext)
+ {
+ objc_setAssociatedObject(self, kNotfiesMainContextKey, [NSNumber numberWithBool:enabled], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ if (enabled)
+ [mainContext observeContextOnMainThread:self];
+ else
+ [mainContext stopObservingContext:self];
+ }
}
+ (NSManagedObjectContext *) contextWithStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator
{
- if (!coordinator)
- return nil;
-
- ARLog(@"Creating MOContext %@", [NSThread isMainThread] ? @" *** On Main Thread ***" : @"");
- NSManagedObjectContext *context = [NSManagedObjectContext new];
- [context setPersistentStoreCoordinator:coordinator];
- return context;
+ if (!coordinator)
+ return nil;
+
+ ARLog(@"Creating MOContext %@", [NSThread isMainThread] ? @" *** On Main Thread ***" : @"");
+ NSManagedObjectContext *context = [NSManagedObjectContext new];
+ [context setPersistentStoreCoordinator:coordinator];
+ return context;
}
+ (NSManagedObjectContext *) contextThatNotifiesDefaultContextOnMainThreadWithCoordinator:(NSPersistentStoreCoordinator *)coordinator;
{
- NSManagedObjectContext *context = [self contextWithStoreCoordinator:coordinator];
- context.notifiesMainContextOnSave = YES;
- return context;
+ NSManagedObjectContext *context = [self contextWithStoreCoordinator:coordinator];
+ context.notifiesMainContextOnSave = YES;
+ return context;
}
+ (NSManagedObjectContext *)context
@@ -210,9 +210,9 @@ + (NSManagedObjectContext *)context
+ (NSManagedObjectContext *) contextThatNotifiesDefaultContextOnMainThread
{
- NSManagedObjectContext *context = [self context];
- context.notifiesMainContextOnSave = YES;
- return context;
+ NSManagedObjectContext *context = [self context];
+ context.notifiesMainContextOnSave = YES;
+ return context;
}
@end
View
26 Source/Categories/NSManagedObjectModel+MagicalRecord.m
@@ -16,39 +16,39 @@ + (NSManagedObjectModel *) defaultManagedObjectModel
{
if (defaultManagedObjectModel_ == nil && [MagicalRecordHelpers shouldAutoCreateManagedObjectModel])
{
- [self setDefaultManagedObjectModel:[self managedObjectModel]];
+ [self setDefaultManagedObjectModel:[self managedObjectModel]];
}
return defaultManagedObjectModel_;
}
+ (void) setDefaultManagedObjectModel:(NSManagedObjectModel *)newDefaultModel
{
- defaultManagedObjectModel_ = newDefaultModel;
+ defaultManagedObjectModel_ = newDefaultModel;
}
+ (NSManagedObjectModel *)managedObjectModel {
- return [self mergedModelFromBundles:nil];
+ return [self mergedModelFromBundles:nil];
}
+ (NSManagedObjectModel *)mergedObjectModelFromMainBundle;
{
- return [self managedObjectModel];
+ return [self managedObjectModel];
}
+ (NSManagedObjectModel *)newManagedObjectModel {
- return [self managedObjectModel];
+ return [self managedObjectModel];
}
+ (NSManagedObjectModel *)newModelNamed:(NSString *) modelName inBundleNamed:(NSString *) bundleName
{
- NSString *path = [[NSBundle mainBundle] pathForResource:[modelName stringByDeletingPathExtension]
- ofType:[modelName pathExtension]
- inDirectory:bundleName];
- NSURL *modelUrl = [NSURL fileURLWithPath:path];
-
- NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelUrl];
-
- return mom;
+ NSString *path = [[NSBundle mainBundle] pathForResource:[modelName stringByDeletingPathExtension]
+ ofType:[modelName pathExtension]
+ inDirectory:bundleName];
+ NSURL *modelUrl = [NSURL fileURLWithPath:path];
+
+ NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelUrl];
+
+ return mom;
}
+ (NSManagedObjectModel *)newManagedObjectModelNamed:(NSString *)modelFileName
View
34 Source/Categories/NSPersistentStore+MagicalRecord.m
@@ -25,8 +25,8 @@ + (void) setDefaultPersistentStore:(NSPersistentStore *) store
}
+ (NSString *)_directory:(NSSearchPathDirectory) type
-{
- return [NSSearchPathForDirectoriesInDomains(type, NSUserDomainMask, YES) lastObject];
+{
+ return [NSSearchPathForDirectoriesInDomains(type, NSUserDomainMask, YES) lastObject];
}
+ (NSString *)_applicationDocumentsDirectory
@@ -37,9 +37,9 @@ + (NSString *)_applicationDocumentsDirectory
+ (NSString *)_applicationLibraryDirectory
{
#if !TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR
- NSString *applicationName = [[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString *)kCFBundleNameKey];
- return [[self _directory:NSApplicationSupportDirectory] stringByAppendingPathComponent:applicationName];
-
+ NSString *applicationName = [[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString *)kCFBundleNameKey];
+ return [[self _directory:NSApplicationSupportDirectory] stringByAppendingPathComponent:applicationName];
+
#else
return [self _directory:NSLibraryDirectory];
#endif
@@ -48,23 +48,23 @@ + (NSString *)_applicationLibraryDirectory
+ (NSURL *)URLForStoreName:(NSString *)storeFileName
{
NSArray *paths = [NSArray arrayWithObjects:[self _applicationDocumentsDirectory], [self _applicationLibraryDirectory], nil];
- NSFileManager *fm = [NSFileManager new];
+ NSFileManager *fm = [NSFileManager new];
- for (NSString *path in paths)
- {
- NSString *filepath = [path stringByAppendingPathComponent:storeFileName];
- if ([fm fileExistsAtPath:filepath])
- {
- return [NSURL fileURLWithPath:filepath];
- }
- }
+ for (NSString *path in paths)
+ {
+ NSString *filepath = [path stringByAppendingPathComponent:storeFileName];
+ if ([fm fileExistsAtPath:filepath])
+ {
+ return [NSURL fileURLWithPath:filepath];
+ }
+ }
- //set default url
- return [NSURL fileURLWithPath:[[self _applicationDocumentsDirectory] stringByAppendingPathComponent:storeFileName]];
+ //set default url
+ return [NSURL fileURLWithPath:[[self _applicationDocumentsDirectory] stringByAppendingPathComponent:storeFileName]];
}
+ (NSURL *)defaultLocalStoreURL {
- return [self URLForStoreName:kMagicalRecordDefaultStoreFileName];
+ return [self URLForStoreName:kMagicalRecordDefaultStoreFileName];
}
@end
View
158 Source/Categories/NSPersistentStoreCoordinator+MagicalRecord.m
@@ -13,77 +13,77 @@ @implementation NSPersistentStoreCoordinator (MagicalRecord)
+ (NSPersistentStoreCoordinator *)defaultStoreCoordinator
{
- if (defaultCoordinator_ == nil && [MagicalRecordHelpers shouldAutoCreateDefaultPersistentStoreCoordinator])
- {
- defaultCoordinator_ = [self newPersistentStoreCoordinator];
- }
+ if (defaultCoordinator_ == nil && [MagicalRecordHelpers shouldAutoCreateDefaultPersistentStoreCoordinator])
+ {
+ defaultCoordinator_ = [self newPersistentStoreCoordinator];
+ }
return defaultCoordinator_;
}
+ (void)setDefaultStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator
{
defaultCoordinator_ = coordinator;