fatbobman / blogComments

1 stars 0 forks source link

SwiftData in WWDC 2024:革命仍在继续、稳定还需时日 #237

Open fatbobman opened 5 months ago

fatbobman commented 5 months ago

SwiftData in WWDC 2024:革命仍在继续、稳定还需时日

SwiftData 自从去年首次亮相,便成为了开发者群体备受关注的焦点框架。随着 WWDC 2024 的来临,业界普遍期待 SwiftData 在功能、性能和稳定性等方面将有突破性进展。本文将评述 SwiftData 最新版本的表现,并分享我在首次体验新版时内心经历的一系列复杂情绪:震惊、喜悦、沮丧以及困惑。

SwiftData in WWDC 2024: The Revolution Continues, Stability Still Awaits

Since its debut last year, SwiftData has become a focal framework that has garnered significant attention from developers. With the arrival of WWDC 2024, there is widespread anticipation for breakthroughs in functionality, performance, and stability in SwiftData. This article will review the performance of the latest version of SwiftData and share the complex emotions I experienced during my first encounter with the new version: shock, joy, disheartened, and perplexed.

alienproX commented 4 months ago

目前也在学习,使用 macos 15 xcode 16 beta3 版本,发现swiftData 始终无法准确同步,主要是同步的信息无法在UI上及时作出反应,但偶尔可以,如果退出App再进入,数据就会刷新。请教老师,这是 beta 版 xocde 的原因吗?

fatbobman commented 4 months ago

估计是 Bug。两个 beta 叠加出现问题的几率会增大很多,可以再等待一下。

inPhilly commented 2 months ago

So where do we go from here? I have a database with a good number of complex to-many relationships, and I would also like to implement sync. Would you recommend Core Data with SwiftUI for now? Can SwiftData even be used for sectionedLists?

TwoSX commented 1 month ago

ModelContext.willSave 和 ModelContext.didSave 终于可以在 iOS 18 上运行了

NotificationCenter.default.publisher(for: ModelContext.didSave)
.sink { output in
  guard let modelContext = output.object as? ModelContext, let userInfo = output.userInfo else {
    return
  }

  let inserted = (userInfo["inserted"] as? [PersistentIdentifier]) ?? []
  let deleted = (userInfo["deleted"] as? [PersistentIdentifier]) ?? []
  let updated = (userInfo["updated"] as? [PersistentIdentifier]) ?? []

  print("inserted: \(inserted), updated: \(updated), deleted: \(deleted)")
}
fatbobman commented 1 month ago

@TwoSX 谢谢告知! 很有意思, willSave 仍不给通知。

TwoSX commented 1 month ago

@TwoSX 谢谢告知! 很有意思, willSave 仍不给通知。

@fatbobman 我这边测试 iOS 18.0 下 willSave 也有通知的

fatbobman commented 1 month ago

@TwoSX 你是 18.0 还是 18.1 , 我在 18.0 下测试,willSave 收不到通知( 无论是模拟器还是真机),18.1 没有测试过

TwoSX commented 1 month ago

@fatbobman 是 18.0。使用 NotificationCenter.default.addObserver 就有。用 publisher 就没有 🤔

NotificationCenter.default.addObserver(
  forName: ModelContext.willSave,
  object: nil,
  queue: nil
) { output in
  if let modelContext = output.object as? ModelContext {
    let changedModelsArray = modelContext.changedModelsArray
    let deletedModelsArray = modelContext.deletedModelsArray
    let insertedModelsArray = modelContext.insertedModelsArray

    print("changedModelsArray: \(changedModelsArray)")
    print("deletedModelsArray: \(deletedModelsArray)")
    print("insertedModelsArray: \(insertedModelsArray)")
  }
}
fatbobman commented 1 month ago

@TwoSX 了解了。看来需要提交个 FB,让苹果尽快改过来

TwoSX commented 1 month ago

@fatbobman 是 18.0。使用 NotificationCenter.default.addObserver 就有。用 publisher 就没有 🤔

NotificationCenter.default.addObserver(
  forName: ModelContext.willSave,
  object: nil,
  queue: nil
) { output in
  if let modelContext = output.object as? ModelContext {
    let changedModelsArray = modelContext.changedModelsArray
    let deletedModelsArray = modelContext.deletedModelsArray
    let insertedModelsArray = modelContext.insertedModelsArray

    print("changedModelsArray: \(changedModelsArray)")
    print("deletedModelsArray: \(deletedModelsArray)")
    print("insertedModelsArray: \(insertedModelsArray)")
  }
}

@TwoSX 了解了。看来需要提交个 FB,让苹果尽快改过来

@fatbobman 是我代码问题。publisher 返回的 cancellable 没有保存,导致被回收了,下面这样测试就正常了。

cancellable = NotificationCenter.default.publisher(for: ModelContext.willSave)
    .receive(on: RunLoop.main)
    .sink { output in
      // 能正常打印
      print("willSave")
    }
fatbobman commented 1 month ago

@TwoSX 有点意思,在视图中使用 onReceive 无法获取到 willSave, 直接通过 Combine 的方式可以获取。还是有 Bug

johnmai-dev commented 1 month ago

刚开始用SwiftData挺方便简单的,你所列的bug我现在都遇到了,我最近一直在纠结要不要回CoreData

fatbobman commented 1 month ago

@maiqingqiang 我目前在新项目中使用的是 Core Data,不过采用了很多 SwiftData 的概念和思想。下周应该会写一篇有关这方面的文章

johnmai-dev commented 1 month ago

@maiqingqiang 我目前在新项目中使用的是 Core Data,不过采用了很多 SwiftData 的概念和思想。下周应该会写一篇有关这方面的文章

期待新文章😄 刷了一波你之前写的 Core Data 文章,收获很多🍻,我最后还是切到Core Data了https://github.com/maiqingqiang/ChatMLX/commit/142bab567d6632ca845504f4ca6e97a78d6c65ee