ポジティブ丸メガネ

3年目エンジニアです。

SwiftのCoredata(データベース)のマイグレーションで少し苦労した。

作成しているアプリでCoredataのAttributeを増やす必要が出てきました。こういった場合、単純にAttributeを増やしてアプリを実行すると、アプリはクラッシュしてしまいます。ですのでCoredataのマイグレーションを行う必要がありますが、少し詰まったので忘れないように残しておきます。

Androidアプリを作成していた頃に、マイグレーションを行った時は非常にめんどうだったので、結局DBを消して無理やり解決していました。今回もそうすれば動くには動くのですが、自分ですでにアプリをそこそこ使っていて、DBをリセットされるのはちょっと残念だったので頑張ってみることにしました。

こちらのサイトを参考にして、行いました。
Core DataでDBのMigrationを行う。 |
こちらのサイトの通りにやっていた(つもり)のですが、下記エラーがでて動きません。

「The managed object model version used to open the persistent store is incompatible with the one that was used to create the persistent store」

ふむ。結構いろいろ調べてみたのですが、大体はDB消してもう一回やれよという強引な解決策がほとんどでした。

もう一度AppDelegateをよくよく見てみると、間違い発見!
try coordinator.addPersistentStoreWithType()のoptionの引数がnilになっていました。元々初期値はnilだったので、治すのを忘れていました。せっかくoptions作ったのに参照してないとかアホすぎでしたね。一応下記に該当部分を載せておきますね。

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
        // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
        // Create the coordinator and store
        let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
        let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
        var failureReason = "There was an error creating or loading the application's saved data."
        
        let options = [NSMigratePersistentStoresAutomaticallyOption: true,NSInferMappingModelAutomaticallyOption: true]
        
        do {
            try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: options)
        } catch {
            // Report any error we got.
            var dict = [String: AnyObject]()
            dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
            dict[NSLocalizedFailureReasonErrorKey] = failureReason

            dict[NSUnderlyingErrorKey] = error as NSError
            let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
            // Replace this with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
            abort()
        }
        
        return coordinator
    }()

まあともかくこれでアプリを実行すると無事マイグレーションが行われていたので、一件落着。

Swift2.0とストーリーボードで自分のアプリにAdmobを導入してみた。

アプリ情報

まずは、開発中のアプリについて。

  • ターゲットOS:iOS8.4
  • Tab Bar Controllerを保有
  • 基本的にTable Viewで情報を表示

ざっくりというとこんな感じです。

ADMOBの導入

akira-watson.com
こちらの情報を元に導入を進めました。ただ、僕の場合はストーリーボードでViewを作成しているので、Viewの作り方やソースコードはこちらを参考にしました。developers.google.com
mzgkworks.hateblo.jp
公式サイトはObjective-Cで記載されていましたので、2つ目のサイトを参考にコードを書きました。

ただ、アプリ起動後、広告を表示しているViewに行くとアプリが落ちました。
「GoogleMobileAds does not contain bitcode・・・」的なことが書いてありましたので、ぐぐってみたところ、こちらのサイトに書いてある通りにすると解決できました。
Cordovaめも: GoogleMobileAds does not contain bitcode

まとめ

Androidアプリを作成している際もAdmobを導入したことがあったのですが、現在はiOSのほうが導入が楽に感じましたね。それは僕が成長したのか、それとも導入が容易になっているのか。前者を期待したいところです。

結局自分で大したことは書いていませんが、上記のサイトをすべて参考にすれば、簡単に導入できましたよ、という話。

JSONの処理に少し時間がかかるのでバックグラウンド処理にしてみた。

kimihiro-n.appspot.com

こちらの記事を参考にして、下記のようにJSONの処理を埋め込みました。

//バックグラウンド処理
     let qualityOfServiceClass = DISPATCH_QUEUE_PRIORITY_DEFAULT
     let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
     dispatch_async(backgroundQueue, {
         // Backgroundで行いたい重い処理はここ
         //JSONの取得処理を行う
         dispatch_async(dispatch_get_main_queue(), {
             // 処理が終わった後UIスレッドでやりたいことはここ
             //内容の表示やボタンが押せるようになるなど
         })
     })