iOS & Swift 공부 - CoreData -> How to View Your SQLite DB Backend for Core Data & Fundamentals

김영채 (Kevin)·2021년 3월 16일
0

iOS & Swift

목록 보기
93/107
post-thumbnail
print(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask))

→ The above code allows you to easily locate where your SQLite file is.

  • Locate the SQLite file and then open it with an SQLite app.
  • We can see that there are attributes that we have added earlier.

Core Data Fundamentals


  • We use different vocabulary depending on what you are using, but essentially, we are talking about the same thing.
  • Class == Entity == Table
  • Property == Attribute == Field

( 3 Entities )


→ If we put these three entities into permanent storage, that becomes our Persistent Container.

→ And this contained is simply a SQLite database that stores all of our tables and all of the relationships between the tables.

  • When we are writing our app, we can't simply "interact" with the persistent store directly.

→ We have to go through a intermediary, which is called Context.

  • Context is like a temporary area where you might create new pieces of data that you want to add to your database.
  • You can create, read, update, or delete. (CRUD)

  • In Core Data, you do CRUD inside the "Context", not directly to the persistent container.

  • Once you've decided that you're happy with what you've done inside the context, do you save the context using " try context.save( ) " .

    → Then, Core Data takes care of the rest and saves what inside the context to the Persistent Container.

    → Very similar to Git.

In summary, in Core Data, we have a persistent contained and our app is NOT allowed to interact directly with a persistent container. Instead, it has to go through this temporary area which is known as the Context.

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

Explanation of the above code:

→ The first thing we create is a constant called "context".

→ This constant goes into the AppDelegate and grabs the persistentContainer.

→ And then we grab a reference to the "context (viewContent)" for that persistentContainer.

NSManagedObject

  • NSManagedObjects are essentially the rows that are inside your table.
  • Every single row will be an individual NSManagedObject.
let newItem = Item(context: self.context)
newItem.title = textField.text!
newItem.isDone = false

→ Then we fill up the fields (attributes or properties).

Then, we save the data.

func saveItems() {
        
        do {
            try context.save()
    
        } catch {
            print("Error saving context \(error)")
        }

        self.tableView.reloadData()
    }
profile
맛있는 iOS 프로그래밍

0개의 댓글