Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
SaveToCoreData
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jon Bent
SaveToCoreData
Commits
0d295ca1
Commit
0d295ca1
authored
Jun 03, 2022
by
Jon Bent
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
First Commit
parent
6443fdd0
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
199 additions
and
12 deletions
+199
-12
project.pbxproj
SaveToCoreData.xcodeproj/project.pbxproj
+21
-0
Contents.json
...CoreData/Assets.xcassets/AppIcon.appiconset/Contents.json
+5
-0
LaunchScreen.storyboard
SaveToCoreData/Base.lproj/LaunchScreen.storyboard
+12
-5
Main.storyboard
SaveToCoreData/Base.lproj/Main.storyboard
+37
-5
ViewController.swift
SaveToCoreData/ViewController.swift
+110
-2
contents
User.xcdatamodeld/User.xcdatamodel/contents
+14
-0
No files found.
SaveToCoreData.xcodeproj/project.pbxproj
View file @
0d295ca1
...
...
@@ -13,6 +13,7 @@
FCE461D02849EFE1004A1782
/* Main.storyboard in Resources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
FCE461CE2849EFE1004A1782
/* Main.storyboard */
;
};
FCE461D22849EFE3004A1782
/* Assets.xcassets in Resources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
FCE461D12849EFE3004A1782
/* Assets.xcassets */
;
};
FCE461D52849EFE3004A1782
/* LaunchScreen.storyboard in Resources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
FCE461D32849EFE3004A1782
/* LaunchScreen.storyboard */
;
};
FCE461DE2849F00A004A1782
/* User.xcdatamodeld in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
FCE461DC2849F00A004A1782
/* User.xcdatamodeld */
;
};
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
...
...
@@ -24,6 +25,7 @@
FCE461D12849EFE3004A1782
/* Assets.xcassets */
=
{
isa
=
PBXFileReference
;
lastKnownFileType
=
folder.assetcatalog
;
path
=
Assets.xcassets
;
sourceTree
=
"<group>"
;
};
FCE461D42849EFE3004A1782
/* Base */
=
{
isa
=
PBXFileReference
;
lastKnownFileType
=
file.storyboard
;
name
=
Base
;
path
=
Base.lproj/LaunchScreen.storyboard
;
sourceTree
=
"<group>"
;
};
FCE461D62849EFE3004A1782
/* Info.plist */
=
{
isa
=
PBXFileReference
;
lastKnownFileType
=
text.plist.xml
;
path
=
Info.plist
;
sourceTree
=
"<group>"
;
};
FCE461DD2849F00A004A1782
/* User.xcdatamodel */
=
{
isa
=
PBXFileReference
;
lastKnownFileType
=
wrapper.xcdatamodel
;
path
=
User.xcdatamodel
;
sourceTree
=
"<group>"
;
};
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
...
...
@@ -40,6 +42,7 @@
FCE461BC2849EFE1004A1782
=
{
isa
=
PBXGroup
;
children
=
(
FCE461DC2849F00A004A1782
/* User.xcdatamodeld */
,
FCE461C72849EFE1004A1782
/* SaveToCoreData */
,
FCE461C62849EFE1004A1782
/* Products */
,
);
...
...
@@ -138,6 +141,7 @@
isa
=
PBXSourcesBuildPhase
;
buildActionMask
=
2147483647
;
files
=
(
FCE461DE2849F00A004A1782
/* User.xcdatamodeld in Sources */
,
FCE461CD2849EFE1004A1782
/* ViewController.swift in Sources */
,
FCE461C92849EFE1004A1782
/* AppDelegate.swift in Sources */
,
FCE461CB2849EFE1004A1782
/* SceneDelegate.swift in Sources */
,
...
...
@@ -295,10 +299,12 @@
INFOPLIST_KEY_UIMainStoryboardFile
=
Main
;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad
=
"UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"
;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone
=
"UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"
;
IPHONEOS_DEPLOYMENT_TARGET
=
15.5
;
LD_RUNPATH_SEARCH_PATHS
=
(
"$(inherited)"
,
"@executable_path/Frameworks"
,
);
MACH_O_TYPE
=
mh_execute
;
MARKETING_VERSION
=
1.0
;
PRODUCT_BUNDLE_IDENTIFIER
=
com.nisum.jonbent.SaveToCoreData
;
PRODUCT_NAME
=
"$(TARGET_NAME)"
;
...
...
@@ -323,10 +329,12 @@
INFOPLIST_KEY_UIMainStoryboardFile
=
Main
;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad
=
"UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"
;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone
=
"UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"
;
IPHONEOS_DEPLOYMENT_TARGET
=
15.5
;
LD_RUNPATH_SEARCH_PATHS
=
(
"$(inherited)"
,
"@executable_path/Frameworks"
,
);
MACH_O_TYPE
=
mh_execute
;
MARKETING_VERSION
=
1.0
;
PRODUCT_BUNDLE_IDENTIFIER
=
com.nisum.jonbent.SaveToCoreData
;
PRODUCT_NAME
=
"$(TARGET_NAME)"
;
...
...
@@ -358,6 +366,19 @@
defaultConfigurationName
=
Release
;
};
/* End XCConfigurationList section */
/* Begin XCVersionGroup section */
FCE461DC2849F00A004A1782
/* User.xcdatamodeld */
=
{
isa
=
XCVersionGroup
;
children
=
(
FCE461DD2849F00A004A1782
/* User.xcdatamodel */
,
);
currentVersion
=
FCE461DD2849F00A004A1782
/* User.xcdatamodel */
;
path
=
User.xcdatamodeld
;
sourceTree
=
"<group>"
;
versionGroupType
=
wrapper.xcdatamodel
;
};
/* End XCVersionGroup section */
};
rootObject
=
FCE461BD2849EFE1004A1782
/* Project object */
;
}
SaveToCoreData/Assets.xcassets/AppIcon.appiconset/Contents.json
View file @
0d295ca1
...
...
@@ -70,6 +70,11 @@
"scale"
:
"2x"
,
"size"
:
"40x40"
},
{
"idiom"
:
"ipad"
,
"scale"
:
"1x"
,
"size"
:
"76x76"
},
{
"idiom"
:
"ipad"
,
"scale"
:
"2x"
,
...
...
SaveToCoreData/Base.lproj/LaunchScreen.storyboard
View file @
0d295ca1
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document
type=
"com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB"
version=
"3.0"
toolsVersion=
"13122.16"
targetRuntime=
"iOS.CocoaTouch"
propertyAccessControl=
"none"
useAutolayout=
"YES"
launchScreen=
"YES"
useTraitCollections=
"YES"
useSafeAreas=
"YES"
colorMatched=
"YES"
initialViewController=
"01J-lp-oVM"
>
<?xml version="1.0" encoding="UTF-8"?>
<document
type=
"com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB"
version=
"3.0"
toolsVersion=
"20037"
targetRuntime=
"iOS.CocoaTouch"
propertyAccessControl=
"none"
useAutolayout=
"YES"
launchScreen=
"YES"
useTraitCollections=
"YES"
useSafeAreas=
"YES"
colorMatched=
"YES"
initialViewController=
"01J-lp-oVM"
>
<device
id=
"retina6_1"
orientation=
"portrait"
appearance=
"light"
/>
<dependencies>
<plugIn
identifier=
"com.apple.InterfaceBuilder.IBCocoaTouchPlugin"
version=
"
13104.12
"
/>
<plugIn
identifier=
"com.apple.InterfaceBuilder.IBCocoaTouchPlugin"
version=
"
20020
"
/>
<capability
name=
"Safe area layout guides"
minToolsVersion=
"9.0"
/>
<capability
name=
"System colors in document resources"
minToolsVersion=
"11.0"
/>
<capability
name=
"documents saved in the Xcode 8 format"
minToolsVersion=
"8.0"
/>
</dependencies>
<scenes>
...
...
@@ -11,10 +13,10 @@
<objects>
<viewController
id=
"01J-lp-oVM"
sceneMemberID=
"viewController"
>
<view
key=
"view"
contentMode=
"scaleToFill"
id=
"Ze5-6b-2t3"
>
<rect
key=
"frame"
x=
"0.0"
y=
"0.0"
width=
"
375"
height=
"667
"
/>
<rect
key=
"frame"
x=
"0.0"
y=
"0.0"
width=
"
414"
height=
"896
"
/>
<autoresizingMask
key=
"autoresizingMask"
widthSizable=
"YES"
heightSizable=
"YES"
/>
<color
key=
"backgroundColor"
xcode11CocoaTouchSystemColor=
"systemBackgroundColor"
cocoaTouchSystemColor=
"whiteColor"
/>
<viewLayoutGuide
key=
"safeArea"
id=
"6Tk-OE-BBY"
/>
<color
key=
"backgroundColor"
systemColor=
"systemBackgroundColor"
/>
</view>
</viewController>
<placeholder
placeholderIdentifier=
"IBFirstResponder"
id=
"iYj-Kq-Ea1"
userLabel=
"First Responder"
sceneMemberID=
"firstResponder"
/>
...
...
@@ -22,4 +24,9 @@
<point
key=
"canvasLocation"
x=
"53"
y=
"375"
/>
</scene>
</scenes>
<resources>
<systemColor
name=
"systemBackgroundColor"
>
<color
white=
"1"
alpha=
"1"
colorSpace=
"custom"
customColorSpace=
"genericGamma22GrayColorSpace"
/>
</systemColor>
</resources>
</document>
SaveToCoreData/Base.lproj/Main.storyboard
View file @
0d295ca1
<?xml version="1.0" encoding="UTF-8"?>
<document
type=
"com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB"
version=
"3.0"
toolsVersion=
"13122.16"
targetRuntime=
"iOS.CocoaTouch"
propertyAccessControl=
"none"
useAutolayout=
"YES"
useTraitCollections=
"YES"
useSafeAreas=
"YES"
colorMatched=
"YES"
initialViewController=
"BYZ-38-t0r"
>
<document
type=
"com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB"
version=
"3.0"
toolsVersion=
"20037"
targetRuntime=
"iOS.CocoaTouch"
propertyAccessControl=
"none"
useAutolayout=
"YES"
useTraitCollections=
"YES"
useSafeAreas=
"YES"
colorMatched=
"YES"
initialViewController=
"BYZ-38-t0r"
>
<device
id=
"retina6_1"
orientation=
"portrait"
appearance=
"light"
/>
<dependencies>
<plugIn
identifier=
"com.apple.InterfaceBuilder.IBCocoaTouchPlugin"
version=
"
13104.12
"
/>
<plugIn
identifier=
"com.apple.InterfaceBuilder.IBCocoaTouchPlugin"
version=
"
20020
"
/>
<capability
name=
"Safe area layout guides"
minToolsVersion=
"9.0"
/>
<capability
name=
"System colors in document resources"
minToolsVersion=
"11.0"
/>
<capability
name=
"documents saved in the Xcode 8 format"
minToolsVersion=
"8.0"
/>
</dependencies>
<scenes>
<!--View Controller-->
<scene
sceneID=
"tne-QT-ifu"
>
<objects>
<viewController
id=
"BYZ-38-t0r"
customClass=
"ViewController"
customModuleProvider=
"target"
sceneMemberID=
"viewController"
>
<viewController
id=
"BYZ-38-t0r"
customClass=
"ViewController"
customModule
=
"SaveToCoreData"
customModule
Provider=
"target"
sceneMemberID=
"viewController"
>
<view
key=
"view"
contentMode=
"scaleToFill"
id=
"8bC-Xf-vdC"
>
<rect
key=
"frame"
x=
"0.0"
y=
"0.0"
width=
"
375"
height=
"667
"
/>
<rect
key=
"frame"
x=
"0.0"
y=
"0.0"
width=
"
414"
height=
"896
"
/>
<autoresizingMask
key=
"autoresizingMask"
widthSizable=
"YES"
heightSizable=
"YES"
/>
<color
key=
"backgroundColor"
xcode11CocoaTouchSystemColor=
"systemBackgroundColor"
cocoaTouchSystemColor=
"whiteColor"
/>
<subviews>
<stackView
opaque=
"NO"
contentMode=
"scaleToFill"
fixedFrame=
"YES"
axis=
"vertical"
translatesAutoresizingMaskIntoConstraints=
"NO"
id=
"J7g-hB-GYZ"
>
<rect
key=
"frame"
x=
"103"
y=
"72"
width=
"208"
height=
"252"
/>
<autoresizingMask
key=
"autoresizingMask"
flexibleMaxX=
"YES"
flexibleMaxY=
"YES"
/>
</stackView>
<button
opaque=
"NO"
contentMode=
"scaleToFill"
fixedFrame=
"YES"
contentHorizontalAlignment=
"center"
contentVerticalAlignment=
"center"
buttonType=
"system"
lineBreakMode=
"middleTruncation"
translatesAutoresizingMaskIntoConstraints=
"NO"
id=
"Mkr-V2-Kaa"
>
<rect
key=
"frame"
x=
"87"
y=
"471"
width=
"100"
height=
"31"
/>
<autoresizingMask
key=
"autoresizingMask"
flexibleMaxX=
"YES"
flexibleMaxY=
"YES"
/>
<state
key=
"normal"
title=
"Button"
/>
<buttonConfiguration
key=
"configuration"
style=
"tinted"
title=
"Clear Users"
/>
<connections>
<action
selector=
"deleteUsers:"
destination=
"BYZ-38-t0r"
eventType=
"touchUpInside"
id=
"CgT-O9-W8h"
/>
</connections>
</button>
<button
opaque=
"NO"
contentMode=
"scaleToFill"
fixedFrame=
"YES"
contentHorizontalAlignment=
"center"
contentVerticalAlignment=
"center"
buttonType=
"system"
lineBreakMode=
"middleTruncation"
translatesAutoresizingMaskIntoConstraints=
"NO"
id=
"JQz-uL-UxP"
>
<rect
key=
"frame"
x=
"227"
y=
"471"
width=
"102"
height=
"31"
/>
<autoresizingMask
key=
"autoresizingMask"
flexibleMaxX=
"YES"
flexibleMaxY=
"YES"
/>
<state
key=
"normal"
title=
"Button"
/>
<buttonConfiguration
key=
"configuration"
style=
"filled"
title=
"Fetch Users"
/>
<connections>
<action
selector=
"fetchUsers:"
destination=
"BYZ-38-t0r"
eventType=
"touchUpInside"
id=
"6HB-Am-40Y"
/>
</connections>
</button>
</subviews>
<viewLayoutGuide
key=
"safeArea"
id=
"6Tk-OE-BBY"
/>
<color
key=
"backgroundColor"
systemColor=
"systemBackgroundColor"
/>
</view>
</viewController>
<placeholder
placeholderIdentifier=
"IBFirstResponder"
id=
"dkx-z0-nzr"
sceneMemberID=
"firstResponder"
/>
</objects>
<point
key=
"canvasLocation"
x=
"-26.086956521739133"
y=
"118.52678571428571"
/>
</scene>
</scenes>
<resources>
<systemColor
name=
"systemBackgroundColor"
>
<color
white=
"1"
alpha=
"1"
colorSpace=
"custom"
customColorSpace=
"genericGamma22GrayColorSpace"
/>
</systemColor>
</resources>
</document>
SaveToCoreData/ViewController.swift
View file @
0d295ca1
...
...
@@ -6,14 +6,122 @@
//
import
UIKit
import
CoreData
class
ViewController
:
UIViewController
{
private
lazy
var
persistentContainer
:
NSPersistentContainer
=
{
NSPersistentContainer
(
name
:
"User"
)
}()
var
managedObjectContext
:
NSManagedObjectContext
?
override
func
viewDidLoad
()
{
super
.
viewDidLoad
()
// Do any additional setup after loading the view.
persistentContainer
.
loadPersistentStores
{
[
weak
self
]
persistentStoreDescription
,
error
in
if
let
error
=
error
{
print
(
"Unable to Add Persistent Store"
)
print
(
"
\(
error
)
,
\(
error
.
localizedDescription
)
"
)
}
else
{
self
?
.
getCoreUsers
()
}
}
}
private
func
getCoreUsers
(){
let
coreDataFetchRequest
=
NSFetchRequest
<
NSFetchRequestResult
>
(
entityName
:
"User"
)
let
users
=
try
?
persistentContainer
.
viewContext
.
fetch
(
coreDataFetchRequest
)
as!
[
User
]
if
let
nonOptUsers
=
users
{
for
user
in
nonOptUsers
{
print
(
user
.
username
)
print
(
user
.
name
)
}
}
}
@IBAction
func
fetchUsers
(
_
sender
:
Any
)
{
let
url
=
URL
(
string
:
"https://jsonplaceholder.typicode.com/users"
)
!
let
managedObjectContext
=
persistentContainer
.
viewContext
URLSession
.
shared
.
fetchData
(
at
:
url
){
(
result
:
Result
<
[
UserModel
],
Error
>
)
in
switch
result
{
case
.
success
(
let
users
):
for
user
in
users
{
let
newUser
=
User
(
context
:
managedObjectContext
)
newUser
.
website
=
user
.
website
newUser
.
name
=
user
.
name
newUser
.
username
=
user
.
username
newUser
.
phone
=
user
.
phone
newUser
.
id
=
Int16
(
user
.
id
)
newUser
.
email
=
user
.
email
print
(
user
)
}
do
{
try
managedObjectContext
.
save
()
}
catch
{
print
(
"unable to save users to core data"
)
}
case
.
failure
(
let
error
):
print
(
error
)
}
}
}
@IBAction
func
deleteUsers
(
_
sender
:
Any
)
{
do
{
let
fetchRequest
:
NSFetchRequest
<
NSFetchRequestResult
>
fetchRequest
=
NSFetchRequest
(
entityName
:
"User"
)
let
deleteRequest
=
NSBatchDeleteRequest
(
fetchRequest
:
fetchRequest
)
deleteRequest
.
resultType
=
.
resultTypeObjectIDs
let
context
=
persistentContainer
.
viewContext
let
batchDelete
=
try
context
.
execute
(
deleteRequest
)
as?
NSBatchDeleteResult
guard
let
deleteResult
=
batchDelete
?
.
result
as?
[
NSManagedObjectID
]
else
{
return
}
let
deletedObjects
:
[
AnyHashable
:
Any
]
=
[
NSDeletedObjectsKey
:
deleteResult
]
NSManagedObjectContext
.
mergeChanges
(
fromRemoteContextSave
:
deletedObjects
,
into
:
[
context
])
}
catch
{
}
}
}
struct
UserModel
:
Decodable
{
let
id
:
Int
let
name
:
String
let
username
:
String
let
email
:
String
let
phone
:
String
let
website
:
String
}
extension
URLSession
{
func
fetchData
(
at
url
:
URL
,
completion
:
@escaping(Result<[UserModel], Error>)
->
Void
)
{
self
.
dataTask
(
with
:
url
)
{
(
data
,
response
,
error
)
in
if
let
error
=
error
{
completion
(
.
failure
(
error
))
}
if
let
data
=
data
{
do
{
let
users
=
try
JSONDecoder
()
.
decode
([
UserModel
]
.
self
,
from
:
data
)
completion
(
.
success
(
users
))
}
catch
let
decoderError
{
completion
(
.
failure
(
decoderError
))
}
}
}
.
resume
()
}
}
User.xcdatamodeld/User.xcdatamodel/contents
0 → 100644
View file @
0d295ca1
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model
type=
"com.apple.IDECoreDataModeler.DataModel"
documentVersion=
"1.0"
lastSavedToolsVersion=
"20086"
systemVersion=
"21F79"
minimumToolsVersion=
"Automatic"
sourceLanguage=
"Swift"
userDefinedModelVersionIdentifier=
""
>
<entity
name=
"User"
representedClassName=
"User"
syncable=
"YES"
codeGenerationType=
"class"
>
<attribute
name=
"email"
optional=
"YES"
attributeType=
"String"
/>
<attribute
name=
"id"
optional=
"YES"
attributeType=
"Integer 16"
defaultValueString=
"0"
usesScalarValueType=
"YES"
/>
<attribute
name=
"name"
optional=
"YES"
attributeType=
"String"
/>
<attribute
name=
"phone"
optional=
"YES"
attributeType=
"String"
/>
<attribute
name=
"username"
optional=
"YES"
attributeType=
"String"
/>
<attribute
name=
"website"
optional=
"YES"
attributeType=
"String"
/>
</entity>
<elements>
<element
name=
"User"
positionX=
"-63"
positionY=
"-18"
width=
"128"
height=
"133"
/>
</elements>
</model>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment