0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

1週間RealmSwiftを勉強してわかった事 Part3

Posted at

今回の内容

  • CRUDのR(Read)➡️Realmに保存しているデータを取得する 
  • CRUDのU(Update)➡️Realmに保存しているデータを更新する

Realmからデータを取得する(Read)

1.import RealmSwift をimportしてなければ、記述しましょう。(エラーがCould not build Objective-C module 'RealmSwift'と表示されたらXcodeの左上の▶️(Ranボタン)でBuildして下さい)

Modelファイル
import RealmSwift 

2.do{}catch{}を用意する。(これでtryの後の関数でErrorが出ても、アプリが落ちないのでcatch{}内の処理が呼ばれます)

3.do{}{}内で、Realm()をインスタンス化します。➡️let realm = try Realm()のことです。

注:class RealmDatas:Object{}を作成した事を仮定して説明を進めます。

4.do{}{}内で、realm.objects(RealmDatas.self)を使用してRealmに保存してあるRealmDatasのデータを全件取得します。取得時は配列の状態で取得されます。

Modelファイル
import RealmSwift 

do{
            let realm = try Realm() //インスタンス化
            let readRealmData = realm.objects(RealmDatas.self)
            
        }catch{
            
            print("!!!ERROE!!!")
            
        }

練習で作ったカレンダーメモアプリで書いたfilterを使ったメソッドもコードだけ載せておきます。(誰もいないと思いますが説明が欲しい方はコメントまでお願いします)

Modelファイル
 var realmReadDatas:[[String:String]] = [] //Realmから取得したデータを入れる為に用意

 func filterReadRealm(selectDate:String){
        
        do {
            let realm = try Realm()
            self.realmReadDatas = []
            
            for filterData in realm.objects(RealmDataSets.self).filter(NSPredicate(format: "realmDay == %@", selectDate)) {
                
                self.realmReadDatas.append(["day":filterData.realmDay,"who":filterData.realmWho,"event":filterData.realmEvent])
                
            }

        }catch{
         //エラー後の処理など
        }
        
    }

Realmのデータを更新する(Update)

1.import RealmSwift をimportしてなければ、記述しましょう。(エラーがCould not build Objective-C module 'RealmSwift'と表示されたらXcodeの左上の▶️(Ranボタン)でBuildして下さい)

Modelファイル
import RealmSwift 

2.do{}catch{}を用意する。(これでtryの後の関数でErrorが出ても、アプリが落ちないのでcatch{}内の処理が呼ばれます)

3.do{}{}内で、Realm()をインスタンス化します。➡️let realm = try Realm()のことです。

4.realm.objects(RealmDataSets.self)を使用してRealmに保存してあるRealmDataSetsのデータを全件取得します。

注:class RealmDatas:Object{}を作成した事を仮定して説明を進めます。

5.try realm.write({})writeトランザクションを作成します。

6.try realm.write({})内で、更新したい値が入ったをreadRealm[Index番号].RealmDataSetsで宣言したプロパティ名 =などで指定をして,そこに新しい値 を入れる事で更新される。

Modelファイル
do{
            let realm = try Realm()
            let readRealm = realm.objects(RealmDataSets.self)
            
            try realm.write({ //writeトランザクション ここから
                
                    readRealm[Index番号].RealmDataSetsで宣言したプロパティ名 = 新しい値 

             }) //writeトランザクション ここまで
            
        }catch{
            //エラー後の処理など   
        }

今回はここまで

  • 次回
  • CRUDのD(Delete) について書きます 

読んでみて間違っている部分などがありましたら、コメントで指摘して頂けると嬉しいです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?