iOS & Swift 공부 - Realm (영)

김영채 (Kevin)·2020년 12월 29일
0

iOS & Swift

목록 보기
3/107
post-thumbnail

Installing RealmSwift


  1. Open Terminal
  2. cd to project directory
  3. pod init (terminal / creates a Podfile)
  4. open Podfile
  5. write: pod 'RealmSwift' right before "end"
  6. save
  7. pod install (terminal)
  8. go back to the project directory and open .xcworkspace file

Explanation


→ Realm is a local database. All of the data you create, store, retrieve are all stored locally on the device.

→ How it's all stored and managed is done in one file: Realm file

[ Realm Studio ] → allows you to browse data stored, add etc

—> need to open a Realm file

how to find the realm file?

  1. add the following line in Xcode (under ViewController.swift → viewDidLoad())
print(Realm.Configuration.defaultConfiguration.fileURL)
  1. Run the app (simulator)

  2. A file directory will be there

  3. open terminal

  4. open ///Users/kevin/Library/Developer/CoreSimulator/Devices/37688EE4-2343-4EA5-8C78-0BE25E8706D0/data/Containers/Data/Application/084E7DBC-723B-47E9-9267-CE5B81163075/Documents/default.realm

(사용자에 따라 디렉토리가 상이함)

  1. Realm Studio will automatically open the file

Actually Using it


import RealmSwift

//make sure that any data or classes that you want to save into the realm
//file subclasses Object

class Cat: Object{

	//to save it into the realm folder, make sure to put
	//@objc dynamic -> these 2 keywords in front
	@objc dynamic var name: String?
	@objc dynamic var color: String?
	@objc dynamic var gender: String?
	
}

let realm = try! Realm()  //make a reference to the realm file

var myCat = Cat() //create new Cat object
myCat.name = "Moe"
myCat.gender = "Male"
myCat.color = "Orange"

//below is how to actually save the data to the realm file
//everytime you need to do something with the realm file, such as
//change, update, delete, etc, you need to realm.write { } (or realm.beginWrite())
try! realm.write{   //we put try! to ignore potential errors
	realm.add(myCat)
}

→ If you're gonna use realm.beginWrite(), you must try! realm.commitWrite() as well

Example:

@objc func didTapSaveButton(){
        
        if let text = textField.text, !text.isEmpty {
            
            let date = datePicker.date
            
            realm.beginWrite()
            
            let newItem = ToDoListItem()        //create new object
            newItem.date = date
            newItem.item = text
            realm.add(newItem)
            
            try! realm.commitWrite()
            
            completionHandler?()
            navigationController?.popToRootViewController(animated: true)
        }
        else{
            print("Add something")
        }
    }

Retrieving Data from Realm file example:


let results = realm.objects(Cat.self)  //returns an array

print(results[0].name)  //구조체이기 때문에 . 으로 접근

//"Moe"

Filter 기능 example:

let results = realm.objects(Cat.self).filter("name = 'Moe'")

print(results.count)  //prints how many
profile
맛있는 iOS 프로그래밍

0개의 댓글