OS&Swift-Realm(英語)の勉強


Installing RealmSwift
  • Open Terminal
  • cd to project directory
  • pod init (terminal/creates a Podfile)
  • open Podfile
  • write: pod 'RealmSwift' right before "end"
  • save
  • pod install (terminal)
  • 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?
  • add the following line in Xcode (under ViewController.swift → viewDidLoad())
  • print(Realm.Configuration.defaultConfiguration.fileURL)

  • Run the app (simulator)

  • A file directory will be there

  • open terminal

  • open///Users/kevin/Library/Developer/CoreSimulator/Devices/37688EE4-2343-4EA5-8C78-0BE25E8706D0/data/Containers/Data/Application/084E7DBC-723B-47E9-9267-CE5B81163075/Documents/default.realm
  • (ユーザーによってディレクトリが異なります)
  • 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機能の例:
    let results = realm.objects(Cat.self).filter("name = 'Moe'")
    
    print(results.count)  //prints how many