update save data for core data

This commit is contained in:
Jungpyo Hong 2021-05-24 13:01:12 -05:00 committed by GitHub
parent d51457fbf2
commit 62fccec752
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,6 +37,8 @@
- [How to find documentDirectory](#How-to-find-documentDirectory)
- [Core Data](#Core-Data)
- [Set Up Core Data](#Set-Up-Core-Data)
- [Usage](#Usage)
- [Store Data](#Store-Data)
- [Third Party Library](#Third-Party-Library)
- [GCD](#GCD)
- [DispatchQueue](#DispatchQueue)
@ -517,6 +519,8 @@ let defaults = UserDefaults.standard
Use Core Data to save your applications permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device.
**Core Data in Swift is using SQLite as DEFAULT**
<img src = "https://github.com/jphong1111/Useful_Swift/blob/main/Images/DataStoreInSwift.png" width = "50%" height = "50%"/>
> Image From London App Brewery
@ -644,6 +648,36 @@ And CoreDataClass, CoreDataProperties are looking like this,
> If your code can run it but didn't get your Entities, **Rebuild it or Restart your Xcode**
## Store Data
Declare context as a global variable
```swift
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
```
> Get viewContext that we defined in AppDelegate.swift file
Simply you can use this code to save your data to CoreData
```swift
func saveItem() {
do {
try context.save()
} catch {
print("Error Saving Context: \(error.localizedDescription)")
}
}
```
> Use it wherever you want
Data can be find if you print the path
```swift
print(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask))
```
<img src = "https://github.com/jphong1111/Useful_Swift/blob/main/Images/CoreDataSQLite.png" />
> You can check Entities, Properties inside that file
**You are GOOD TO GO** 👏👏👏
## Third Party Library