【SwiftData】 データストアファイルを SQLite コマンドで見てみる

そもそものきっかけは、

「Preview と シュミレーター の保存データは違うのか」

という素朴な疑問から。

 

📂 データファイルはどこにあるのか

xcrun で調べたみたが、パスが深くて長いし、

デバイスやアプリのIDがあれこれ違うので面倒すぎる。

コード内から吐かすのが楽ちん。


FileManager
  .default
  .urls(for: .applicationSupportDirectory, in: .userDomainMask)
  .last!
  .path(percentEncoded: false)

👉 Where does SwiftData store the data? | Apple Developer Forums hatena-bookmark

とか、いや、


URL.applicationSupportDirectory
  .path(percentEncoded: false)

で取得できるので print() などで出力。


// Playground
/Users/{USER_NAME}/Library/
  Application Support/

// Preview
/Users/{USER_NAME}/Library/Developer/
  Xcode/UserData/Previews/
  Simulator Devices/{DEVICE_ID}/
  data/Containers/Data/Application/{APP_ID}/Library/
  Application Support/

// Simulator
/Users/{USER_NAME}/Library/Developer/
  CoreSimulator/
  Devices/{DEVICE_ID}/
  data/Containers/Data/Application/{APP_ID}/Library/
  Application Support/

見づらいので改行しています。

実行環境で違うのか。

それぞれの「Application Support」 ディレクトリ以下に該当ファイルがある。


default.store
default.store-shm
default.store-wal

 

📂 SQLite コマンドで見てみる

👉 sqlite — Homebrew Formulae hatena-bookmark

せっかくなので覗く。


$ sqlite default.store

SQLite version 3.43.2 2023-10-10 13:08:14
Enter ".help" for usage hints.

sqlite> .tables
ACHANGE             ATRANSACTIONSTRING  ZTODO               Z_MODELCACHE
ATRANSACTION        Z_METADATA          Z_PRIMARYKEY

sqlite> .schema ZTODO
CREATE TABLE ZTODO ( Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER, Z_OPT INTEGER, ZTIME TIMESTAMP, ZTEXT VARCHAR );

sqlite> .headers on
sqlite> .mode column

sqlite> select * from ZTODO;
Z_PK  Z_ENT  Z_OPT  ZTIME             ZTEXT
----  -----  -----  ----------------  -----------------------------------------------------------
11    2      1      727759392.308872  2. Do, or do not. There is no try.
12    2      1      727759392.914907  3. Most things look better when you put them in a circle.
13    2      1      727759393.191901  4. Could not get advice.
14    2      1      727760485.069984  0. You're not that important; it's what you do that counts.
15    2      1      727760485.781123  1. Most things done in secrecy are better left undone.
16    2      1      727760486.503812  2. When in doubt, just take the next small step.
17    2      1      727760487.112024  3. Have a firm handshake.

よくできてるなあ、SwiftData って。




【 #SwiftData 】ModelContainer の View へのセット

ModelContainer セット時には便利な、

view extension と modifier のセットですが、

複数に重なってくると、

なんだか頭が痛くなる。

👉 sample-backyard-birds/BackyardBirdsData/General/BackyardBirdsDataContainer.swift at 1843d5655bf884b501e2889ad9862ec58978fdbe · apple/sample-backyard-birds hatena-bookmark

再度、ModelContainer の基本的な利用の流れを整理しておきます。

 

👨‍💻 ModelContainer から ViewModifier を作成


struct DataContainerViewModifier: ViewModifier {
  private let container: ModelContainer
    
  init() {
    container = try! ModelContainer( 
      for: Schema([Plant.self, Bird.self]),
      configurations: [ModelConfiguration()]
    )
  }
    
  func body(content: Content) -> some View {
    content
      .modelContainer(container)
  }
}

 

👨‍💻 ViewModifier から extension View を作成


extension View {
  func dataContainer() -> some View {
    modifier(DataContainerViewModifier())
  }
}

これ、#Preview など利用したい View で簡単に利用できて便利です。

 

👨‍💻 View にセットする


@main
struct SampleApp: App {
  var body: some Scene {
    WindowGroup {
      ContentView()
        .dataContainer()
    }
  }
}

これで この View 以下の View では、

@Query, @Environment を利用して、

データ取得、監視、操作が行えるようになりました。

 

👨‍💻 元コードを編集してみる

元のサンプルコードは、

以上の流れが、2層になっているので、

1層にして、少し見通し良くしてみました。

作成後の ModelContainer の ModelContext は、


container.mainContext

で、直接取得することができます。

 

👨‍💻 まとめ

SwiftData の基本的な構成を図でまとめておきます。

👉 Preserving your app’s model data across launches | Apple Developer Documentation hatena-bookmark

手順的には、


1. Scheme (@Model) と ModelConfiguration を作成する。

2. それらを使って ModelContainer を作成する。

3. それを使って extension View を作成する。

4. それを View にセットする。

というかんじで定型的にサンプルコードでは書いてます。

以下、参考記事。

👉 Dive deeper into SwiftData - WWDC23 - Videos - Apple Developer hatena-bookmark
👉 SwiftData Stack: Understanding Schema, Container & Context - swiftyplace hatena-bookmark
👉 View を拡張したい場合は原則として extension を使用し、状態保持が必要な場合のみ `ViewModifier` を実装する。 · YusukeHosonuma/Effective-SwiftUI · Discussion #31 hatena-bookmark

👉 【SwiftUI + SwiftData】List のアイテムの Preview hatena-bookmark


【Swift】「public」 を省略しない理由 🚫

iOS アプリの Apple 公式ドキュメントや著名作者のコードをあれこれ眺めていて気になっていたのは、

「なんで public を省略しないのか」

ということ。


public extension Image {
    static let fountain = Image(.fountain)
    static let fountainFill = Image(.fountainFill)
}

👉 sample-backyard-birds/BackyardBirdsUI/Images.swift at 1843d5655bf884b501e2889ad9862ec58978fdbe · apple/sample-backyard-birds hatena-bookmark

「はっきり明示する。」

のがポリシーなのかと思ったが省略してる箇所もある。

なんでなの ?

 

🚫 '***' is inaccessible due to 'internal' protection level

試しに消してみると、


extension Image {
    static let fountain = Image(.fountain)
    static let fountainFill = Image(.fountainFill)
}

呼んでいる場所でエラー発生。


'fountain' is inaccessible due to 'internal' protection level

見えてはいるけどアクセスができない。

 

🚫 まとめ

Java など他の言語とは違います。

アクセスレベルを省略した場合は「internal」。

同じモジュール内からしかアクセスできません。

移民はつらい。

しかし、さすが公式サンプルコードは勉強になります。

しっかりしています。

👉 Access Control | Documentation hatena-bookmark


【SwiftUI】今どきの データモデル (Model data) のマクロ記述 📝

ネットで参考になりそうなコード記述を探すと、

新旧入り乱れてる感じがしたので、

まず読んでおいたほうがいいような気がした。

👉 Platforms State of the Union (ASL) - WWDC23 - Videos - Apple Developer hatena-bookmark

公式ドキュメントにもしっかり説明があるようなので、ざっくり古いかもしれない記述を整理しておく。


Managing user interface state | Apple Developer Documentation
👉 https://developer.apple.com/documentation/swiftui/managing-user-interface-state

Managing model data in your app | Apple Developer Documentation
👉 https://developer.apple.com/documentation/swiftui/managing-model-data-in-your-app

Migrating from the Observable Object protocol to the Observable macro | Apple Developer Documentation
👉 https://developer.apple.com/documentation/swiftui/migrating-from-the-observable-object-protocol-to-the-observable-macro

 

📝 まとめ


: ObservableObject   →   @Observable
@Published           →   不要
@StateObject         →   @State
.environmentObject() →   .environment()
@EnvironmentObject   →   @Environment
@ObservedObject      →   不要 or @Bindable

→ 「@Published*Object があれば古い。」

SwiftData を使うにしても、まずは SwiftUI のみの記述の変遷の知識もいるよな ?

先人たちの作ったネットリソースや AI系コードサジェスチョンを効率的に利用するためにも。

次は SwiftData です。


【SwiftUI】シンプルに HTTPリクエスト でお天気情報取得

単純に GET によるレスポンスボディを取得したい。

こんなにシンプルな感じでかけるとは!


let url = URL(string: "https://example.com")!
for try await line in url.lines {
  print(line)
}

👉 task(priority:_:) | Apple Developer Documentation hatena-bookmark

一行ごとに取れるようです。

試してみます。


import SwiftUI
import PlaygroundSupport

struct SampleView: View {
  @State private var message = "Loading..."

  var body: some View {
    Text(message)
      .task {
        message = await getWeather()
      }
  }

  private func getWeather() async -> String {
    let url = URL(string: "https://wttr.in/?format=3")!

    do {
      var lines: [String] = []
      for try await line in url.lines {
        lines.append(line)
      }
      return lines.joined()
    } catch {
      return "Faild to load"
    }
  }
}

PlaygroundPage.current.setLiveView(
  SampleView()
    .frame(width: 300, height: 300)
)

簡単にレスポンスを確認するときに使えそうです。

👉 lines | Apple Developer Documentation hatena-bookmark
👉 chubin/wttr.in: :partly_sunny: The right way to check the weather hatena-bookmark

 

😅 まさか String() でこんなことができるとか


print(
  (try? String(contentsOf: URL(string: "https://wttr.in/?format=3")!)) 
    ?? "Error!"
)

// Kisarazu, Japan: ⛅️  +25°C