├── .gitignore ├── CoreDataTutorialPart1Final.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ ├── xcshareddata │ │ └── IDEWorkspaceChecks.plist │ └── xcuserdata │ │ ├── j.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jamesrochabrun.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ ├── j.xcuserdatad │ └── xcschemes │ │ └── xcschememanagement.plist │ └── jamesrochabrun.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── CoreDataTutorialPart1Final.xcscheme │ └── xcschememanagement.plist ├── CoreDataTutorialPart1Final ├── APIService.swift ├── AppDelegate.swift ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ ├── Contents.json │ └── placeholder.imageset │ │ ├── Contents.json │ │ └── placeholder.png ├── Base.lproj │ └── LaunchScreen.storyboard ├── CoreDataStack.swift ├── CoreDataTutorialPart1Final.xcdatamodeld │ ├── .xccurrentversion │ └── CoreDataTutorialPart1Final.xcdatamodel │ │ └── contents ├── Extensions.swift ├── Info.plist ├── Photo+CoreDataClass.swift ├── Photo+CoreDataProperties.swift ├── PhotoCell.swift └── PhotoVC.swift ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.xcuserdatad 2 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 7B09F2421E68C8F700C8774B /* APIService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B09F2411E68C8F700C8774B /* APIService.swift */; }; 11 | 7B4DA3571E6B9DB4008CFF8A /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B4DA3561E6B9DB4008CFF8A /* Extensions.swift */; }; 12 | 7BD171381E6782130012FF66 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BD171371E6782130012FF66 /* AppDelegate.swift */; }; 13 | 7BD171401E6782130012FF66 /* CoreDataTutorialPart1Final.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 7BD1713E1E6782130012FF66 /* CoreDataTutorialPart1Final.xcdatamodeld */; }; 14 | 7BD171421E6782130012FF66 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7BD171411E6782130012FF66 /* Assets.xcassets */; }; 15 | 7BD171451E6782130012FF66 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7BD171431E6782130012FF66 /* LaunchScreen.storyboard */; }; 16 | 7BD171501E6782750012FF66 /* PhotoVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BD1714F1E6782750012FF66 /* PhotoVC.swift */; }; 17 | 7BD171521E6782800012FF66 /* PhotoCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BD171511E6782800012FF66 /* PhotoCell.swift */; }; 18 | 7BD171551E6792420012FF66 /* Photo+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BD171531E6792420012FF66 /* Photo+CoreDataClass.swift */; }; 19 | 7BD171561E6792420012FF66 /* Photo+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BD171541E6792420012FF66 /* Photo+CoreDataProperties.swift */; }; 20 | 7BD171581E679CE80012FF66 /* CoreDataStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BD171571E679CE80012FF66 /* CoreDataStack.swift */; }; 21 | /* End PBXBuildFile section */ 22 | 23 | /* Begin PBXFileReference section */ 24 | 7B09F2411E68C8F700C8774B /* APIService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIService.swift; sourceTree = ""; }; 25 | 7B4DA3561E6B9DB4008CFF8A /* Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = ""; }; 26 | 7BD171341E6782130012FF66 /* CoreDataTutorialPart1Final.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CoreDataTutorialPart1Final.app; sourceTree = BUILT_PRODUCTS_DIR; }; 27 | 7BD171371E6782130012FF66 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 28 | 7BD1713F1E6782130012FF66 /* CoreDataTutorialPart1Final.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = CoreDataTutorialPart1Final.xcdatamodel; sourceTree = ""; }; 29 | 7BD171411E6782130012FF66 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 30 | 7BD171441E6782130012FF66 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 31 | 7BD171461E6782130012FF66 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 32 | 7BD1714F1E6782750012FF66 /* PhotoVC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhotoVC.swift; sourceTree = ""; }; 33 | 7BD171511E6782800012FF66 /* PhotoCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhotoCell.swift; sourceTree = ""; }; 34 | 7BD171531E6792420012FF66 /* Photo+CoreDataClass.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Photo+CoreDataClass.swift"; sourceTree = ""; }; 35 | 7BD171541E6792420012FF66 /* Photo+CoreDataProperties.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Photo+CoreDataProperties.swift"; sourceTree = ""; }; 36 | 7BD171571E679CE80012FF66 /* CoreDataStack.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreDataStack.swift; sourceTree = ""; }; 37 | /* End PBXFileReference section */ 38 | 39 | /* Begin PBXFrameworksBuildPhase section */ 40 | 7BD171311E6782130012FF66 /* Frameworks */ = { 41 | isa = PBXFrameworksBuildPhase; 42 | buildActionMask = 2147483647; 43 | files = ( 44 | ); 45 | runOnlyForDeploymentPostprocessing = 0; 46 | }; 47 | /* End PBXFrameworksBuildPhase section */ 48 | 49 | /* Begin PBXGroup section */ 50 | 7BD1712B1E6782130012FF66 = { 51 | isa = PBXGroup; 52 | children = ( 53 | 7BD171361E6782130012FF66 /* CoreDataTutorialPart1Final */, 54 | 7BD171351E6782130012FF66 /* Products */, 55 | ); 56 | sourceTree = ""; 57 | }; 58 | 7BD171351E6782130012FF66 /* Products */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | 7BD171341E6782130012FF66 /* CoreDataTutorialPart1Final.app */, 62 | ); 63 | name = Products; 64 | sourceTree = ""; 65 | }; 66 | 7BD171361E6782130012FF66 /* CoreDataTutorialPart1Final */ = { 67 | isa = PBXGroup; 68 | children = ( 69 | 7B09F2411E68C8F700C8774B /* APIService.swift */, 70 | 7BD171371E6782130012FF66 /* AppDelegate.swift */, 71 | 7B4DA3561E6B9DB4008CFF8A /* Extensions.swift */, 72 | 7BD1714C1E6782500012FF66 /* Model */, 73 | 7BD1714D1E67825B0012FF66 /* View */, 74 | 7BD1714E1E6782620012FF66 /* Controller */, 75 | 7BD171411E6782130012FF66 /* Assets.xcassets */, 76 | 7BD171431E6782130012FF66 /* LaunchScreen.storyboard */, 77 | 7BD171461E6782130012FF66 /* Info.plist */, 78 | 7BD1713E1E6782130012FF66 /* CoreDataTutorialPart1Final.xcdatamodeld */, 79 | ); 80 | path = CoreDataTutorialPart1Final; 81 | sourceTree = ""; 82 | }; 83 | 7BD1714C1E6782500012FF66 /* Model */ = { 84 | isa = PBXGroup; 85 | children = ( 86 | 7BD171531E6792420012FF66 /* Photo+CoreDataClass.swift */, 87 | 7BD171541E6792420012FF66 /* Photo+CoreDataProperties.swift */, 88 | 7BD171571E679CE80012FF66 /* CoreDataStack.swift */, 89 | ); 90 | name = Model; 91 | sourceTree = ""; 92 | }; 93 | 7BD1714D1E67825B0012FF66 /* View */ = { 94 | isa = PBXGroup; 95 | children = ( 96 | 7BD171511E6782800012FF66 /* PhotoCell.swift */, 97 | ); 98 | name = View; 99 | sourceTree = ""; 100 | }; 101 | 7BD1714E1E6782620012FF66 /* Controller */ = { 102 | isa = PBXGroup; 103 | children = ( 104 | 7BD1714F1E6782750012FF66 /* PhotoVC.swift */, 105 | ); 106 | name = Controller; 107 | sourceTree = ""; 108 | }; 109 | /* End PBXGroup section */ 110 | 111 | /* Begin PBXNativeTarget section */ 112 | 7BD171331E6782130012FF66 /* CoreDataTutorialPart1Final */ = { 113 | isa = PBXNativeTarget; 114 | buildConfigurationList = 7BD171491E6782130012FF66 /* Build configuration list for PBXNativeTarget "CoreDataTutorialPart1Final" */; 115 | buildPhases = ( 116 | 7BD171301E6782130012FF66 /* Sources */, 117 | 7BD171311E6782130012FF66 /* Frameworks */, 118 | 7BD171321E6782130012FF66 /* Resources */, 119 | ); 120 | buildRules = ( 121 | ); 122 | dependencies = ( 123 | ); 124 | name = CoreDataTutorialPart1Final; 125 | productName = CoreDataTutorialPart1Final; 126 | productReference = 7BD171341E6782130012FF66 /* CoreDataTutorialPart1Final.app */; 127 | productType = "com.apple.product-type.application"; 128 | }; 129 | /* End PBXNativeTarget section */ 130 | 131 | /* Begin PBXProject section */ 132 | 7BD1712C1E6782130012FF66 /* Project object */ = { 133 | isa = PBXProject; 134 | attributes = { 135 | LastSwiftUpdateCheck = 0810; 136 | LastUpgradeCheck = 1030; 137 | ORGANIZATIONNAME = "James Rochabrun"; 138 | TargetAttributes = { 139 | 7BD171331E6782130012FF66 = { 140 | CreatedOnToolsVersion = 8.1; 141 | DevelopmentTeam = ULU46F325W; 142 | LastSwiftMigration = 1030; 143 | ProvisioningStyle = Automatic; 144 | }; 145 | }; 146 | }; 147 | buildConfigurationList = 7BD1712F1E6782130012FF66 /* Build configuration list for PBXProject "CoreDataTutorialPart1Final" */; 148 | compatibilityVersion = "Xcode 3.2"; 149 | developmentRegion = en; 150 | hasScannedForEncodings = 0; 151 | knownRegions = ( 152 | en, 153 | Base, 154 | ); 155 | mainGroup = 7BD1712B1E6782130012FF66; 156 | productRefGroup = 7BD171351E6782130012FF66 /* Products */; 157 | projectDirPath = ""; 158 | projectRoot = ""; 159 | targets = ( 160 | 7BD171331E6782130012FF66 /* CoreDataTutorialPart1Final */, 161 | ); 162 | }; 163 | /* End PBXProject section */ 164 | 165 | /* Begin PBXResourcesBuildPhase section */ 166 | 7BD171321E6782130012FF66 /* Resources */ = { 167 | isa = PBXResourcesBuildPhase; 168 | buildActionMask = 2147483647; 169 | files = ( 170 | 7BD171451E6782130012FF66 /* LaunchScreen.storyboard in Resources */, 171 | 7BD171421E6782130012FF66 /* Assets.xcassets in Resources */, 172 | ); 173 | runOnlyForDeploymentPostprocessing = 0; 174 | }; 175 | /* End PBXResourcesBuildPhase section */ 176 | 177 | /* Begin PBXSourcesBuildPhase section */ 178 | 7BD171301E6782130012FF66 /* Sources */ = { 179 | isa = PBXSourcesBuildPhase; 180 | buildActionMask = 2147483647; 181 | files = ( 182 | 7BD171581E679CE80012FF66 /* CoreDataStack.swift in Sources */, 183 | 7B4DA3571E6B9DB4008CFF8A /* Extensions.swift in Sources */, 184 | 7BD171561E6792420012FF66 /* Photo+CoreDataProperties.swift in Sources */, 185 | 7BD171551E6792420012FF66 /* Photo+CoreDataClass.swift in Sources */, 186 | 7BD171521E6782800012FF66 /* PhotoCell.swift in Sources */, 187 | 7BD171381E6782130012FF66 /* AppDelegate.swift in Sources */, 188 | 7BD171401E6782130012FF66 /* CoreDataTutorialPart1Final.xcdatamodeld in Sources */, 189 | 7B09F2421E68C8F700C8774B /* APIService.swift in Sources */, 190 | 7BD171501E6782750012FF66 /* PhotoVC.swift in Sources */, 191 | ); 192 | runOnlyForDeploymentPostprocessing = 0; 193 | }; 194 | /* End PBXSourcesBuildPhase section */ 195 | 196 | /* Begin PBXVariantGroup section */ 197 | 7BD171431E6782130012FF66 /* LaunchScreen.storyboard */ = { 198 | isa = PBXVariantGroup; 199 | children = ( 200 | 7BD171441E6782130012FF66 /* Base */, 201 | ); 202 | name = LaunchScreen.storyboard; 203 | sourceTree = ""; 204 | }; 205 | /* End PBXVariantGroup section */ 206 | 207 | /* Begin XCBuildConfiguration section */ 208 | 7BD171471E6782130012FF66 /* Debug */ = { 209 | isa = XCBuildConfiguration; 210 | buildSettings = { 211 | ALWAYS_SEARCH_USER_PATHS = NO; 212 | CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; 213 | CLANG_ANALYZER_NONNULL = YES; 214 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 215 | CLANG_CXX_LIBRARY = "libc++"; 216 | CLANG_ENABLE_MODULES = YES; 217 | CLANG_ENABLE_OBJC_ARC = YES; 218 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 219 | CLANG_WARN_BOOL_CONVERSION = YES; 220 | CLANG_WARN_COMMA = YES; 221 | CLANG_WARN_CONSTANT_CONVERSION = YES; 222 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 223 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 224 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 225 | CLANG_WARN_EMPTY_BODY = YES; 226 | CLANG_WARN_ENUM_CONVERSION = YES; 227 | CLANG_WARN_INFINITE_RECURSION = YES; 228 | CLANG_WARN_INT_CONVERSION = YES; 229 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 230 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 231 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 232 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 233 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 234 | CLANG_WARN_STRICT_PROTOTYPES = YES; 235 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 236 | CLANG_WARN_SUSPICIOUS_MOVES = YES; 237 | CLANG_WARN_UNREACHABLE_CODE = YES; 238 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 239 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 240 | COPY_PHASE_STRIP = NO; 241 | DEBUG_INFORMATION_FORMAT = dwarf; 242 | ENABLE_STRICT_OBJC_MSGSEND = YES; 243 | ENABLE_TESTABILITY = YES; 244 | GCC_C_LANGUAGE_STANDARD = gnu99; 245 | GCC_DYNAMIC_NO_PIC = NO; 246 | GCC_NO_COMMON_BLOCKS = YES; 247 | GCC_OPTIMIZATION_LEVEL = 0; 248 | GCC_PREPROCESSOR_DEFINITIONS = ( 249 | "DEBUG=1", 250 | "$(inherited)", 251 | ); 252 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 253 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 254 | GCC_WARN_UNDECLARED_SELECTOR = YES; 255 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 256 | GCC_WARN_UNUSED_FUNCTION = YES; 257 | GCC_WARN_UNUSED_VARIABLE = YES; 258 | IPHONEOS_DEPLOYMENT_TARGET = 10.1; 259 | MTL_ENABLE_DEBUG_INFO = YES; 260 | ONLY_ACTIVE_ARCH = YES; 261 | SDKROOT = iphoneos; 262 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 263 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 264 | }; 265 | name = Debug; 266 | }; 267 | 7BD171481E6782130012FF66 /* Release */ = { 268 | isa = XCBuildConfiguration; 269 | buildSettings = { 270 | ALWAYS_SEARCH_USER_PATHS = NO; 271 | CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; 272 | CLANG_ANALYZER_NONNULL = YES; 273 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 274 | CLANG_CXX_LIBRARY = "libc++"; 275 | CLANG_ENABLE_MODULES = YES; 276 | CLANG_ENABLE_OBJC_ARC = YES; 277 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 278 | CLANG_WARN_BOOL_CONVERSION = YES; 279 | CLANG_WARN_COMMA = YES; 280 | CLANG_WARN_CONSTANT_CONVERSION = YES; 281 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 282 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 283 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 284 | CLANG_WARN_EMPTY_BODY = YES; 285 | CLANG_WARN_ENUM_CONVERSION = YES; 286 | CLANG_WARN_INFINITE_RECURSION = YES; 287 | CLANG_WARN_INT_CONVERSION = YES; 288 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 289 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 290 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 291 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 292 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 293 | CLANG_WARN_STRICT_PROTOTYPES = YES; 294 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 295 | CLANG_WARN_SUSPICIOUS_MOVES = YES; 296 | CLANG_WARN_UNREACHABLE_CODE = YES; 297 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 298 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 299 | COPY_PHASE_STRIP = NO; 300 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 301 | ENABLE_NS_ASSERTIONS = NO; 302 | ENABLE_STRICT_OBJC_MSGSEND = YES; 303 | GCC_C_LANGUAGE_STANDARD = gnu99; 304 | GCC_NO_COMMON_BLOCKS = YES; 305 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 306 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 307 | GCC_WARN_UNDECLARED_SELECTOR = YES; 308 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 309 | GCC_WARN_UNUSED_FUNCTION = YES; 310 | GCC_WARN_UNUSED_VARIABLE = YES; 311 | IPHONEOS_DEPLOYMENT_TARGET = 10.1; 312 | MTL_ENABLE_DEBUG_INFO = NO; 313 | SDKROOT = iphoneos; 314 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 315 | VALIDATE_PRODUCT = YES; 316 | }; 317 | name = Release; 318 | }; 319 | 7BD1714A1E6782130012FF66 /* Debug */ = { 320 | isa = XCBuildConfiguration; 321 | buildSettings = { 322 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 323 | DEVELOPMENT_TEAM = ULU46F325W; 324 | INFOPLIST_FILE = CoreDataTutorialPart1Final/Info.plist; 325 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 326 | PRODUCT_BUNDLE_IDENTIFIER = com.speedelo.CoreDataTutorialPart1Final; 327 | PRODUCT_NAME = "$(TARGET_NAME)"; 328 | SWIFT_VERSION = 5.0; 329 | }; 330 | name = Debug; 331 | }; 332 | 7BD1714B1E6782130012FF66 /* Release */ = { 333 | isa = XCBuildConfiguration; 334 | buildSettings = { 335 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 336 | DEVELOPMENT_TEAM = ULU46F325W; 337 | INFOPLIST_FILE = CoreDataTutorialPart1Final/Info.plist; 338 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 339 | PRODUCT_BUNDLE_IDENTIFIER = com.speedelo.CoreDataTutorialPart1Final; 340 | PRODUCT_NAME = "$(TARGET_NAME)"; 341 | SWIFT_VERSION = 5.0; 342 | }; 343 | name = Release; 344 | }; 345 | /* End XCBuildConfiguration section */ 346 | 347 | /* Begin XCConfigurationList section */ 348 | 7BD1712F1E6782130012FF66 /* Build configuration list for PBXProject "CoreDataTutorialPart1Final" */ = { 349 | isa = XCConfigurationList; 350 | buildConfigurations = ( 351 | 7BD171471E6782130012FF66 /* Debug */, 352 | 7BD171481E6782130012FF66 /* Release */, 353 | ); 354 | defaultConfigurationIsVisible = 0; 355 | defaultConfigurationName = Release; 356 | }; 357 | 7BD171491E6782130012FF66 /* Build configuration list for PBXNativeTarget "CoreDataTutorialPart1Final" */ = { 358 | isa = XCConfigurationList; 359 | buildConfigurations = ( 360 | 7BD1714A1E6782130012FF66 /* Debug */, 361 | 7BD1714B1E6782130012FF66 /* Release */, 362 | ); 363 | defaultConfigurationIsVisible = 0; 364 | defaultConfigurationName = Release; 365 | }; 366 | /* End XCConfigurationList section */ 367 | 368 | /* Begin XCVersionGroup section */ 369 | 7BD1713E1E6782130012FF66 /* CoreDataTutorialPart1Final.xcdatamodeld */ = { 370 | isa = XCVersionGroup; 371 | children = ( 372 | 7BD1713F1E6782130012FF66 /* CoreDataTutorialPart1Final.xcdatamodel */, 373 | ); 374 | currentVersion = 7BD1713F1E6782130012FF66 /* CoreDataTutorialPart1Final.xcdatamodel */; 375 | path = CoreDataTutorialPart1Final.xcdatamodeld; 376 | sourceTree = ""; 377 | versionGroupType = wrapper.xcdatamodel; 378 | }; 379 | /* End XCVersionGroup section */ 380 | }; 381 | rootObject = 7BD1712C1E6782130012FF66 /* Project object */; 382 | } 383 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final.xcodeproj/project.xcworkspace/xcuserdata/j.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jamesrochabrun/CoreDataTutorialPart1Final/148056214bda958e81f0fdffafbd1fce0c04aceb/CoreDataTutorialPart1Final.xcodeproj/project.xcworkspace/xcuserdata/j.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final.xcodeproj/project.xcworkspace/xcuserdata/jamesrochabrun.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jamesrochabrun/CoreDataTutorialPart1Final/148056214bda958e81f0fdffafbd1fce0c04aceb/CoreDataTutorialPart1Final.xcodeproj/project.xcworkspace/xcuserdata/jamesrochabrun.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final.xcodeproj/xcuserdata/j.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | CoreDataTutorialPart1Final.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final.xcodeproj/xcuserdata/jamesrochabrun.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 14 | 15 | 16 | 18 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final.xcodeproj/xcuserdata/jamesrochabrun.xcuserdatad/xcschemes/CoreDataTutorialPart1Final.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final.xcodeproj/xcuserdata/jamesrochabrun.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | CoreDataTutorialPart1Final.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 7BD171331E6782130012FF66 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/APIService.swift: -------------------------------------------------------------------------------- 1 | // 2 | // APIservice.swift 3 | // CoreDataTutorialPart1Final 4 | // 5 | // Created by James Rochabrun on 3/2/17. 6 | // Copyright © 2017 James Rochabrun. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | 12 | class APIService: NSObject { 13 | 14 | let query = "dogs" 15 | lazy var endPoint: String = { 16 | return "https://api.flickr.com/services/feeds/photos_public.gne?format=json&tags=\(self.query)&nojsoncallback=1#" 17 | }() 18 | 19 | func getDataWith(completion: @escaping (Result<[[String: AnyObject]]>) -> Void) { 20 | 21 | let urlString = endPoint 22 | 23 | guard let url = URL(string: urlString) else { return completion(.Error("Invalid URL, we can't update your feed")) } 24 | 25 | URLSession.shared.dataTask(with: url) { (data, response, error) in 26 | 27 | guard error == nil else { return completion(.Error(error!.localizedDescription)) } 28 | guard let data = data else { return completion(.Error(error?.localizedDescription ?? "There are no new Items to show")) 29 | } 30 | do { 31 | if let json = try JSONSerialization.jsonObject(with: data, options: [.mutableContainers]) as? [String: AnyObject] { 32 | guard let itemsJsonArray = json["items"] as? [[String: AnyObject]] else { 33 | return completion(.Error(error?.localizedDescription ?? "There are no new Items to show")) 34 | } 35 | DispatchQueue.main.async { 36 | completion(.Success(itemsJsonArray)) 37 | } 38 | } 39 | } catch let error { 40 | return completion(.Error(error.localizedDescription)) 41 | } 42 | }.resume() 43 | } 44 | } 45 | 46 | enum Result { 47 | case Success(T) 48 | case Error(String) 49 | } 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // CoreDataTutorialPart1Final 4 | // 5 | // Created by James Rochabrun on 3/1/17. 6 | // Copyright © 2017 James Rochabrun. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import CoreData 11 | 12 | @UIApplicationMain 13 | class AppDelegate: UIResponder, UIApplicationDelegate { 14 | 15 | var window: UIWindow? 16 | 17 | 18 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 19 | window = UIWindow(frame: UIScreen.main.bounds) 20 | window?.makeKeyAndVisible() 21 | let photoVC = PhotoVC() 22 | window?.rootViewController = UINavigationController(rootViewController: photoVC) 23 | CoreDataStack.sharedInstance.applicationDocumentsDirectory() 24 | return true 25 | } 26 | 27 | func applicationWillResignActive(_ application: UIApplication) { 28 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 29 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 30 | } 31 | 32 | func applicationDidEnterBackground(_ application: UIApplication) { 33 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 34 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 35 | } 36 | 37 | func applicationWillEnterForeground(_ application: UIApplication) { 38 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 39 | } 40 | 41 | func applicationDidBecomeActive(_ application: UIApplication) { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | func applicationWillTerminate(_ application: UIApplication) { 46 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 47 | // Saves changes in the application's managed object context before the application terminates. 48 | CoreDataStack.sharedInstance.saveContext() 49 | } 50 | 51 | 52 | 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | } 43 | ], 44 | "info" : { 45 | "version" : 1, 46 | "author" : "xcode" 47 | } 48 | } -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/Assets.xcassets/placeholder.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "scale" : "2x" 10 | }, 11 | { 12 | "idiom" : "universal", 13 | "filename" : "placeholder.png", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/Assets.xcassets/placeholder.imageset/placeholder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jamesrochabrun/CoreDataTutorialPart1Final/148056214bda958e81f0fdffafbd1fce0c04aceb/CoreDataTutorialPart1Final/Assets.xcassets/placeholder.imageset/placeholder.png -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/CoreDataStack.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CoreDataStack.swift 3 | // CoreDataTutorialPart1Final 4 | // 5 | // Created by James Rochabrun on 3/1/17. 6 | // Copyright © 2017 James Rochabrun. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | import CoreData 12 | 13 | class CoreDataStack: NSObject { 14 | 15 | static let sharedInstance = CoreDataStack() 16 | private override init() {} 17 | 18 | // MARK: - Core Data stack 19 | lazy var persistentContainer: NSPersistentContainer = { 20 | /* 21 | The persistent container for the application. This implementation 22 | creates and returns a container, having loaded the store for the 23 | application to it. This property is optional since there are legitimate 24 | error conditions that could cause the creation of the store to fail. 25 | */ 26 | let container = NSPersistentContainer(name: "CoreDataTutorialPart1Final") 27 | container.loadPersistentStores(completionHandler: { (storeDescription, error) in 28 | if let error = error as NSError? { 29 | // Replace this implementation with code to handle the error appropriately. 30 | // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 31 | 32 | /* 33 | Typical reasons for an error here include: 34 | * The parent directory does not exist, cannot be created, or disallows writing. 35 | * The persistent store is not accessible, due to permissions or data protection when the device is locked. 36 | * The device is out of space. 37 | * The store could not be migrated to the current model version. 38 | Check the error message to determine what the actual problem was. 39 | */ 40 | fatalError("Unresolved error \(error), \(error.userInfo)") 41 | } 42 | }) 43 | return container 44 | }() 45 | 46 | // MARK: - Core Data Saving support 47 | 48 | func saveContext () { 49 | let context = persistentContainer.viewContext 50 | if context.hasChanges { 51 | do { 52 | try context.save() 53 | } catch { 54 | // Replace this implementation with code to handle the error appropriately. 55 | // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 56 | let nserror = error as NSError 57 | fatalError("Unresolved error \(nserror), \(nserror.userInfo)") 58 | } 59 | } 60 | } 61 | } 62 | 63 | 64 | 65 | extension CoreDataStack { 66 | 67 | func applicationDocumentsDirectory() { 68 | // The directory the application uses to store the Core Data store file. This code uses a directory named "yo.BlogReaderApp" in the application's documents directory. 69 | if let url = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).last { 70 | print(url.absoluteString) 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/CoreDataTutorialPart1Final.xcdatamodeld/.xccurrentversion: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | _XCCurrentVersionName 6 | CoreDataTutorialPart1Final.xcdatamodel 7 | 8 | 9 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/CoreDataTutorialPart1Final.xcdatamodeld/CoreDataTutorialPart1Final.xcdatamodel/contents: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/Extensions.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Extensions.swift 3 | // CoreDataTutorial 4 | // 5 | // Created by James Rochabrun on 3/4/17. 6 | // Copyright © 2017 James Rochabrun. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | 12 | let imageCache = NSCache() 13 | 14 | extension UIImageView { 15 | 16 | func loadImageUsingCacheWithURLString(_ URLString: String, placeHolder: UIImage?) { 17 | 18 | self.image = nil 19 | if let cachedImage = imageCache.object(forKey: NSString(string: URLString)) { 20 | self.image = cachedImage 21 | return 22 | } 23 | 24 | if let url = URL(string: URLString) { 25 | URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in 26 | 27 | //print("RESPONSE FROM API: \(response)") 28 | if error != nil { 29 | print("ERROR LOADING IMAGES FROM URL: \(String(describing: error))") 30 | DispatchQueue.main.async { 31 | self.image = placeHolder 32 | } 33 | return 34 | } 35 | DispatchQueue.main.async { 36 | if let data = data { 37 | if let downloadedImage = UIImage(data: data) { 38 | imageCache.setObject(downloadedImage, forKey: NSString(string: URLString)) 39 | self.image = downloadedImage 40 | } 41 | } 42 | } 43 | }).resume() 44 | } 45 | } 46 | } 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIRequiredDeviceCapabilities 26 | 27 | armv7 28 | 29 | UISupportedInterfaceOrientations 30 | 31 | UIInterfaceOrientationPortrait 32 | UIInterfaceOrientationLandscapeLeft 33 | UIInterfaceOrientationLandscapeRight 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/Photo+CoreDataClass.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Photo+CoreDataClass.swift 3 | // CoreDataTutorialPart1Final 4 | // 5 | // Created by James Rochabrun on 3/1/17. 6 | // Copyright © 2017 James Rochabrun. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import CoreData 11 | 12 | 13 | public class Photo: NSManagedObject { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/Photo+CoreDataProperties.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Photo+CoreDataProperties.swift 3 | // CoreDataTutorialPart1Final 4 | // 5 | // Created by James Rochabrun on 3/1/17. 6 | // Copyright © 2017 James Rochabrun. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import CoreData 11 | 12 | 13 | extension Photo { 14 | 15 | @nonobjc public class func fetchRequest() -> NSFetchRequest { 16 | return NSFetchRequest(entityName: "Photo"); 17 | } 18 | 19 | @NSManaged public var author: String? 20 | @NSManaged public var tags: String? 21 | @NSManaged public var mediaURL: String? 22 | 23 | } 24 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/PhotoCell.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PhotoCell.swift 3 | // CoreDataTutorial 4 | // 5 | // Created by James Rochabrun on 3/1/17. 6 | // Copyright © 2017 James Rochabrun. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | 12 | 13 | class PhotoCell: UITableViewCell { 14 | 15 | 16 | let photoImageview: UIImageView = { 17 | let iv = UIImageView() 18 | iv.contentMode = .scaleAspectFill 19 | iv.translatesAutoresizingMaskIntoConstraints = false 20 | iv.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1) 21 | iv.layer.masksToBounds = true 22 | return iv 23 | }() 24 | 25 | let authorLabel: UILabel = { 26 | let label = UILabel() 27 | label.textColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1) 28 | //label.backgroundColor = #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1) 29 | label.font = UIFont.systemFont(ofSize: 16) 30 | label.translatesAutoresizingMaskIntoConstraints = false 31 | label.numberOfLines = 0 32 | label.textAlignment = .center 33 | label.lineBreakMode = .byWordWrapping 34 | return label 35 | }() 36 | 37 | let tagsLabel: UILabel = { 38 | let label = UILabel() 39 | label.textColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1) 40 | //label.backgroundColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1) 41 | label.font = UIFont.systemFont(ofSize: 14) 42 | label.numberOfLines = 0 43 | label.lineBreakMode = .byWordWrapping 44 | label.textAlignment = .center 45 | label.translatesAutoresizingMaskIntoConstraints = false 46 | return label 47 | }() 48 | 49 | let dividerLineView: UIView = { 50 | let v = UIView() 51 | v.backgroundColor = UIColor(white: 0.4, alpha: 0.4) 52 | v.translatesAutoresizingMaskIntoConstraints = false 53 | return v 54 | }() 55 | 56 | 57 | override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { 58 | super.init(style: style, reuseIdentifier: reuseIdentifier) 59 | isUserInteractionEnabled = false 60 | 61 | addSubview(photoImageview) 62 | addSubview(authorLabel) 63 | addSubview(tagsLabel) 64 | addSubview(dividerLineView) 65 | 66 | photoImageview.topAnchor.constraint(equalTo: topAnchor).isActive = true 67 | photoImageview.leftAnchor.constraint(equalTo: leftAnchor).isActive = true 68 | photoImageview.widthAnchor.constraint(equalTo: widthAnchor).isActive = true 69 | photoImageview.heightAnchor.constraint(equalTo: widthAnchor).isActive = true 70 | 71 | authorLabel.heightAnchor.constraint(equalToConstant: 30).isActive = true 72 | authorLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: -14).isActive = true 73 | authorLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: 14).isActive = true 74 | authorLabel.topAnchor.constraint(equalTo: photoImageview.bottomAnchor).isActive = true 75 | 76 | dividerLineView.heightAnchor.constraint(equalToConstant: 1).isActive = true 77 | dividerLineView.leftAnchor.constraint(equalTo: leftAnchor, constant: 14).isActive = true 78 | dividerLineView.rightAnchor.constraint(equalTo: rightAnchor, constant: -14).isActive = true 79 | dividerLineView.topAnchor.constraint(equalTo: authorLabel.bottomAnchor).isActive = true 80 | 81 | tagsLabel.heightAnchor.constraint(equalToConstant: 69).isActive = true 82 | tagsLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: -14).isActive = true 83 | tagsLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: 14).isActive = true 84 | tagsLabel.topAnchor.constraint(equalTo: dividerLineView.bottomAnchor).isActive = true 85 | 86 | } 87 | 88 | required init?(coder aDecoder: NSCoder) { 89 | fatalError("init(coder:) has not been implemented") 90 | } 91 | 92 | func setPhotoCellWith(photo: Photo) { 93 | 94 | DispatchQueue.main.async { 95 | self.authorLabel.text = photo.author 96 | self.tagsLabel.text = photo.tags 97 | if let url = photo.mediaURL { 98 | self.photoImageview.loadImageUsingCacheWithURLString(url, placeHolder: UIImage(named: "placeholder")) 99 | } 100 | } 101 | } 102 | } 103 | 104 | 105 | -------------------------------------------------------------------------------- /CoreDataTutorialPart1Final/PhotoVC.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // CoreDataTutorial 4 | // 5 | // Created by James Rochabrun on 3/1/17. 6 | // Copyright © 2017 James Rochabrun. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import CoreData 11 | 12 | 13 | 14 | class PhotoVC: UITableViewController { 15 | 16 | private let cellID = "cellID" 17 | 18 | lazy var fetchedhResultController: NSFetchedResultsController = { 19 | let fetchRequest = NSFetchRequest(entityName: String(describing: Photo.self)) 20 | fetchRequest.sortDescriptors = [NSSortDescriptor(key: "author", ascending: true)] 21 | let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: CoreDataStack.sharedInstance.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil) 22 | frc.delegate = self 23 | return frc 24 | }() 25 | 26 | override func viewDidLoad() { 27 | super.viewDidLoad() 28 | self.title = "Photos Feed" 29 | view.backgroundColor = .white 30 | tableView.register(PhotoCell.self, forCellReuseIdentifier: cellID) 31 | updateTableContent() 32 | } 33 | 34 | func updateTableContent() { 35 | 36 | do { 37 | try self.fetchedhResultController.performFetch() 38 | print("COUNT FETCHED FIRST: \(String(describing: self.fetchedhResultController.sections?[0].numberOfObjects))") 39 | } catch let error { 40 | print("ERROR: \(error)") 41 | } 42 | 43 | let service = APIService() 44 | service.getDataWith { (result) in 45 | switch result { 46 | case .Success(let data): 47 | self.clearData() 48 | self.saveInCoreDataWith(array: data) 49 | case .Error(let message): 50 | DispatchQueue.main.async { 51 | self.showAlertWith(title: "Error", message: message) 52 | } 53 | } 54 | } 55 | } 56 | 57 | func showAlertWith(title: String, message: String, style: UIAlertController.Style = .alert) { 58 | 59 | let alertController = UIAlertController(title: title, message: message, preferredStyle: style) 60 | let action = UIAlertAction(title: title, style: .default) { (action) in 61 | self.dismiss(animated: true, completion: nil) 62 | } 63 | alertController.addAction(action) 64 | self.present(alertController, animated: true, completion: nil) 65 | } 66 | 67 | override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 68 | let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! PhotoCell 69 | 70 | if let photo = fetchedhResultController.object(at: indexPath) as? Photo { 71 | cell.setPhotoCellWith(photo: photo) 72 | } 73 | return cell 74 | } 75 | 76 | override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 77 | 78 | if let count = fetchedhResultController.sections?.first?.numberOfObjects { 79 | return count 80 | } 81 | return 0 82 | } 83 | 84 | override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 85 | return view.frame.width + 100 //100 = sum of labels height + height of divider line 86 | } 87 | 88 | private func createPhotoEntityFrom(dictionary: [String: AnyObject]) -> NSManagedObject? { 89 | 90 | let context = CoreDataStack.sharedInstance.persistentContainer.viewContext 91 | if let photoEntity = NSEntityDescription.insertNewObject(forEntityName: "Photo", into: context) as? Photo { 92 | photoEntity.author = dictionary["author"] as? String 93 | photoEntity.tags = dictionary["tags"] as? String 94 | let mediaDictionary = dictionary["media"] as? [String: AnyObject] 95 | photoEntity.mediaURL = mediaDictionary?["m"] as? String 96 | return photoEntity 97 | } 98 | return nil 99 | } 100 | 101 | private func saveInCoreDataWith(array: [[String: AnyObject]]) { 102 | _ = array.map{self.createPhotoEntityFrom(dictionary: $0)} 103 | do { 104 | try CoreDataStack.sharedInstance.persistentContainer.viewContext.save() 105 | } catch let error { 106 | print(error) 107 | } 108 | } 109 | 110 | private func clearData() { 111 | do { 112 | 113 | let context = CoreDataStack.sharedInstance.persistentContainer.viewContext 114 | let fetchRequest = NSFetchRequest(entityName: String(describing: Photo.self)) 115 | do { 116 | let objects = try context.fetch(fetchRequest) as? [NSManagedObject] 117 | _ = objects.map{$0.map{context.delete($0)}} 118 | CoreDataStack.sharedInstance.saveContext() 119 | } catch let error { 120 | print("ERROR DELETING : \(error)") 121 | } 122 | } 123 | } 124 | } 125 | 126 | 127 | extension PhotoVC: NSFetchedResultsControllerDelegate { 128 | 129 | func controller(_ controller: NSFetchedResultsController, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) { 130 | 131 | switch type { 132 | case .insert: 133 | self.tableView.insertRows(at: [newIndexPath!], with: .automatic) 134 | case .delete: 135 | self.tableView.deleteRows(at: [indexPath!], with: .automatic) 136 | default: 137 | break 138 | } 139 | } 140 | 141 | func controllerDidChangeContent(_ controller: NSFetchedResultsController) { 142 | self.tableView.endUpdates() 143 | } 144 | 145 | func controllerWillChangeContent(_ controller: NSFetchedResultsController) { 146 | tableView.beginUpdates() 147 | } 148 | } 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 James Rochabrun 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CoreDataTutorialPart1Final 2 | Final project of CoreData tutorial part 1 3 | 4 | https://medium.com/@jamesrochabrun/parsing-json-response-and-save-it-in-coredata-step-by-step-fb58fc6ce16f 5 | 6 | ![jcd](https://user-images.githubusercontent.com/5378604/48667165-3e34d580-ea85-11e8-8e2c-ba58a3b2ecc6.png) 7 | --------------------------------------------------------------------------------