๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/Third Party

[iOS/Library] Swift Realm์œผ๋กœ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌํ•˜๊ธฐ (Realm DataBase Save,Read,Update,Delete)

by Fomagran ๐Ÿ’ป 2020. 4. 5.
728x90
๋ฐ˜์‘ํ˜•

๋ ˜์ด๋ž€? 

์•„์ฃผ ๊ฐ„ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋ฐ”์ผ ์šฉ ์˜คํ”ˆ์†Œ์Šค ๊ฐ์ฒด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ž…๋‹ˆ๋‹ค

์‚ฌ์šฉ๋ฒ•์€ ์šฐ์„  pod์— 'ReamSwift'๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

ํ„ฐ๋ฏธ๋„๋กœ ๊ฐ€์„œ pod install์„ ํ•ด์ค€ ๋’ค

๋‹ค์‹œ Xcode๋กœ ์™€์„œ 'import RealmSwift'๋ฅผ ํ•ด์ค€ ๋’ค ์›ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ์ €๋Š” UserInfo๋ผ๊ณ  ์ด๋ฆ„ ์ง“๊ณ  (๋ฐ˜๋“œ์‹œ ์˜†์— Object๋กœ ๋ช…์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.)

์ด๋ฆ„๊ณผ ๋‚˜์ด ์„ฑ๋ณ„์„ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์„ ์–ธํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
import Foundation
import RealmSwift
 
class UserInfo:Object {
    @objc dynamic var name = ""
    @objc dynamic var age = 0
    @objc dynamic var gender = ""
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ์— ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ import RealmSwift๋ฅผ ํ•ด์ค€ ๋’ค ๋ ˜์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์œ ์ €์ธํฌ๋ฅผ ๋งŒ๋“ค์–ด ์ค„ ๋ฉ”์†Œ๋“œ๋กœ makeUserInfo๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
  func makeUserInfo(_ name:String,_ age:Int,_ gender:String-> UserInfo {
        let userinfo = UserInfo()
        userinfo.name = name
        userinfo.age = age
        userinfo.gender = gender
        
        return userinfo
    }
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

์šฐ์„  ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ž์‹ ์ด ์›ํ•˜๋Š” ์œ ์ €์ธํฌ๊ฐ€ ๋‹ด๊ธด ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ 

๋ ˜์ธ์Šคํ„ด์Šค์— ์ถ”๊ฐ€ ํ•ด์ค๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
    @IBAction func save(_ sender: Any) {
        let ๋นˆ์ง€๋…ธ = self.makeUserInfo("Beenzino",34,"๋‚จ")
        let ๋‚˜ํ”Œ๋ผ = self.makeUserInfo("Nafla"29,"๋‚จ")
          let ์œค๋ฏธ๋ž˜ = self.makeUserInfo("Tasha",40,"์—ฌ")
        
        try! realmInstance.write{
        }
        self.label.text = "์ €์žฅ๋์Šต๋‹ˆ๋‹ค!!"       
    }
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

๋‘ ๋ฒˆ์งธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๋Š” ๋ฐฉ๋ฒ•์€ ๋ ˜์ธ์Šคํ„ด์Šค์— ์žˆ๋Š” ์˜ค๋ธŒ์ ํŠธ๋“ค ์ค‘ ์œ ์ €์ธํฌ๋กœ ๋œ ๊ฒƒ๋“ค์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

 
1
2
3
4
5
6
7
8
   @IBAction func read(_ sender: Any) {
        let rappers = realmInstance.objects(UserInfo.self)
        var rappersname = String()
        for i in 0..<rappers.count {
        rappersname += "\(rappers[i].name)\n"
         self.label.text = rappersname
    }
    }
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

๊ทธ ๋‹ค์Œ ์—…๋ฐ์ดํŠธ๋ฅผ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€NSPredicate๋ฅผ ์ด์šฉํ•ด ํ…์ŠคํŠธํ•„๋“œ์— ์ ํžŒ ์ด๋ฆ„์„ ์ฐพ์•„์„œ ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด์˜ ๋‚ด์šฉ์„ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
    @IBAction func update(_ sender: Any) {
        if  let userinfo = realmInstance.objects(UserInfo.self).filter(NSPredicate(format: "name = %@"textfield.text ?? "No Rapper")).first {
                        try! realmInstance.write {
                            userinfo.name = "ํƒ€์ด๊ฑฐjk"
                                userinfo.age = 47
                                userinfo.gender = "๋‚จ"
                        }
                    }else{
            self.label.text = "์—†๋Š”๋ฐ์š”??"
                    }
        
    }
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

๋งˆ์ง€๋ง‰์œผ๋กœ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ NSPredicate๋ฅผ ์ด์šฉํ•ด ํ…์ŠคํŠธํ•„๋“œ์— ์ ํžŒ ์ด๋ฆ„์„ ๋ ˜์ธ์Šคํ„ด์Šค์˜ ์˜ค๋ธŒ์ ํŠธ์—์„œ ํ•ด๋‹น ์œ ์ €์ธํฌ๋ฅผ ์‚ญ์ œ์‹œ์ผœ์ค๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
    @IBAction func deletedata(_ sender: Any) {
        if  let userinfo = realmInstance.objects(UserInfo.self).filter(NSPredicate(format: "name = %@"textfield.text ?? "No Rapper")).first {
                  try! realmInstance.write {
                      realmInstance.delete(userinfo)
                  }
            self.label.text = "์‚ญ์ œ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!!"
              }else{
                  print("์—†๋Š”๋ฐ์š”??")
              }
    }
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

์•„๋ž˜๋Š” ๊ฐ ๊ฐ ์ €์žฅ,์—…๋ฐ์ดํŠธ,์ฝ๊ธฐ,์‚ญ์ œ๋ฅผ ์‹คํ–‰ํ•œ ํ™”๋ฉด์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด fileURL์„ ์•Œ์•„๋‚ธ ๋’ค

1
print(Realm.Configuration.defaultConfiguration.fileURL)

ํ•ด๋‹น ์ฃผ์†Œ๋กœ ์ด๋™ํ•˜๋ฉด

์•„๋ž˜์™€ ๊ฐ™์ด ๋ ˜ํŒŒ์ผ์ด ์žˆ๋Š”๋ฐ ํด๋ฆญํ•ด์ฃผ๋ฉด

๋ฐฉ๊ธˆ ์ €์žฅํ•œ ๋ฐ์ดํ„ฐ๋“ค์„ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!!!

์˜ค๋Š˜์€ ๋ ˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์•Œ์•„๋ณด์•˜๋Š”๋ฐ ํ˜น์‹œ๋ผ๋„ ํ‹€๋ฆฐ ์ ์ด ์žˆ๊ฑฐ๋‚˜ ๊ถ๊ธˆํ•˜์‹  ์‚ฌํ•ญ์€ ์–ธ์ œ๋“  ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์„ธ์š”~

์ „์ฒด์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import UIKit
import RealmSwift
 
 
class TabbarViewController1: UIViewController,UITextFieldDelegate {
    
    let realmInstance = try! Realm()
    
    @IBOutlet weak var textfield: UITextField!
    @IBOutlet weak var delete: UIButton!
    @IBOutlet weak var read: UIButton!
    @IBOutlet weak var update: UIButton!
    @IBOutlet weak var save: UIButton!
    @IBOutlet weak var stack: UIStackView!
    @IBOutlet weak var label: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        print(Realm.Configuration.defaultConfiguration.fileURL)
       
    }
    
    func makeUserInfo(_ name:String,_ age:Int,_ gender:String-> UserInfo {
        let userinfo = UserInfo()
        userinfo.name = name
        userinfo.age = age
        userinfo.gender = gender
        
        return userinfo
    }
 
    @IBAction func save(_ sender: Any) {
        let ๋นˆ์ง€๋…ธ = self.makeUserInfo("Beenzino",34,"๋‚จ")
        let ๋‚˜ํ”Œ๋ผ = self.makeUserInfo("Nafla"29,"๋‚จ")
          let ์œค๋ฏธ๋ž˜ = self.makeUserInfo("Tasha",40,"์—ฌ")
        
        try! realmInstance.write{
        }
        self.label.text = "์ €์žฅ๋์Šต๋‹ˆ๋‹ค!!"
    }
    @IBAction func update(_ sender: Any) {
        if  let userinfo = realmInstance.objects(UserInfo.self).filter(NSPredicate(format: "name = %@"textfield.text ?? "No Rapper")).first {
                        try! realmInstance.write {
                            userinfo.name = "ํƒ€์ด๊ฑฐjk"
                                userinfo.age = 47
                                userinfo.gender = "๋‚จ"
                        }
                    }else{
            self.label.text = "์—†๋Š”๋ฐ์š”??"
                    }
        
    }
    @IBAction func read(_ sender: Any) {
        let rappers = realmInstance.objects(UserInfo.self)
        var rappersname = String()
        for i in 0..<rappers.count {
        rappersname += "\(rappers[i].name)\n"
         self.label.text = rappersname
    }
    }
        
    @IBAction func deletedata(_ sender: Any) {
        if  let userinfo = realmInstance.objects(UserInfo.self).filter(NSPredicate(format: "name = %@"textfield.text ?? "No Rapper")).first {
                  try! realmInstance.write {
                      realmInstance.delete(userinfo)
                  }
            self.label.text = "์‚ญ์ œ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!!"
              }else{
                  print("์—†๋Š”๋ฐ์š”??")
              }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€