Share
Embed
Backport AsyncImage
集まれSwift好き!Swift愛好会 vol.61 @ オンライン https://love-swift.connpass.com/event/218651/
Yutaro Muta
July 21, 2021
Transcript
31 slides
-
• Yutaro Muta @yutailang0119 • Hatena Co., Ltd. @Kyoto •
Conference Sta f Who am I ? -
Goal • SwiftUI.AsyncImage ΛΔ • ࠓ (Xcode 12.5.1) ͔Β͑Δ SBPAsyncImage
• SBPAsyncImage ͔ΒֶͿ SwiftUI Trick -
Environment • Xcode Version 13.0 beta 3 (13A5192i) • Xcode
Version 12.5.1 (12E507) -
SwiftUI.AsyncImage • > A view that asynchronously loads and displays
an image. • Available on Xcode 13 struct AsyncImage<Content> where Content : View https://developer.apple.com/documentation/swiftui/asyncimage -
SwiftUI.AsyncImage AsyncImage(url: URL(string: “https://example.com/icon.png")) .frame(width: 200, height: 200) AsyncImage(url: URL(string:
"https://example.com/icon.png")) { image in image.resizable() } placeholder: { ProgressView() } .frame(width: 50, height: 50) -
SwiftUI.AsyncImage Xcode 13 beta 3ݱࡏ • ⚠ URLRequestʹະରԠ • ϦΫΤετຖʹΩϟογϡϙϦγʔλΠϜΞτͳͲΛίϯτϩʔϧ
Ͱ͖ͳ͍ • ⚠ తͳը૾ͷΩϟογϡػೳͳ͍ -
• ⚠ Availability • iOS 15.0+ Beta • macOS 12.0+
Beta • Mac Catalyst 15.0+ Beta • tvOS 15.0+ Beta • watchOS 8.0+ Beta SwiftUI.AsyncImage -
yutailang0119/SBPAsyncImage • > Backport of SwiftUI.AsyncImage to iOS 14, macOS
11, tvOS 14 and watchOS 7 and earlier. > SBPAsyncImage provides interface and behavior of AsyncImage to earlier OS. • Available on Xcode 12.5.1 struct BackportAsyncImage<Content> where Content : View typealias AsyncImage = BackportAsyncImage https://github.com/yutailang0119/SBPAsyncImage -
yutailang0119/SBPAsyncImage BackportAsyncImage(url: URL(string: “https://example.com/icon.png")) .frame(width: 200, height: 200) SBPAsyncImage.AsyncImage(url: URL(string:
"https://example.com/icon.png")) { image in image.resizable() } placeholder: { ProgressView() } .frame(width: 50, height: 50) -
Availability • iOS 13.0+ • macOS 10.5+ • Mac Catalyst
13.0+ • tvOS 13.0+ • watchOS 6.0+ yutailang0119/SBPAsyncImage -
Availability • iOS 13.0+ • macOS 10.5+ • Mac Catalyst
13.0+ • tvOS 13.0+ • watchOS 6.0+ yutailang0119/SBPAsyncImage ✅ Cover all SwiftUI platforms -
yutailang0119/SBPAsyncImage • ⚠ SwiftUI.AsyncImage ΛશʹτϨʔεͰ͖͍ͯͳ͍ • ྫ͑ɺ௨৴ͷΩϟϯηϧ • SwiftUI.AsyncImage
task(_:) Ͱ௨৴Λ։͍࢝ͯͦ͠͏ • ⚠ macOS ͰScaleʹରԠͰ͖͍ͯͳ͍ • https://github.com/yutailang0119/SBPAsyncImage/issues/8 • Welcome Pull Request 🥴 -
Case1: @ObservableObject ඳըຖʹinit͞ΕΔ • @ObservableObject Ͱఆٛ͢ΔϓϩύςΟɺRootͷView͕ඳը͞ΕΔ ຖʹinit͞ΕΔ • ObservedObject ͕࣋ͭอ࣋͞Εͳ͍
https://developer.apple.com/documentation/combine/observableobject -
• https://github.com/yutailang0119/SBPAsyncImage/pull/5 ҎલɺҎԼͷ Α͏ͳ͍ํΛ͢ΔͱɺϘλϯΛԡ͢ʹ௨৴͞Ε͍ͯͨ • ϘλϯΛԡ͢ʹɺҰը૾͕ۭʹͳΔ struct ContentView: View {
@State var count: Int = 0 var body: some View { VStack { SBPAsyncImage.AsyncImage(url: URL(string: "https://example.com/icon.png")) Button { count += 1 } label: { Text("Count: \(count)") } } } } Case1: @ObservableObject ඳըຖʹinit͞ΕΔ -
• @StateObject Λͬͯղܾ • ⚠ Availability • iOS 14.0+ •
macOS 11.0+ • Mac Catalyst 14.0+ • tvOS 14.0+ • watchOS 7.0+ https://developer.apple.com/documentation/swiftui/stateobject Case1: @ObservableObject ඳըຖʹinit͞ΕΔ -
• @StateObject Λ͑ͳ͍ҎલͷOSͰ @ObservableObject ͱ @State ΛΈ߹ΘͤͯɺৼΔ͍Λ฿͢Δ • ͘Θ͘͠ `iOS
13ͰStateObject͕͍͍ͨʂ` • https://kouki.hatenadiary.com/entry/2021/06/22/130000 Case1: @ObservableObject ඳըຖʹinit͞ΕΔ -
• StateObject.init(wrappedValue:) • > You don’t call this initializer directly.
Instead, declare a property with the @StateObject attribute in a View, App, or Scene, and provide an initial value: • initΛ࣮ߦ͢ΔͱɺSwiftUIͷϝϞϦཧ @autoclosure ͷ࠷దԽ͕ ޮ͔ͳ͘ͳΔ https://developer.apple.com/documentation/swiftui/stateobject/init(wrappedvalue:) Case2: StateObject Λinit͍͚ͯ͠ͳ͍ init(wrappedValue thunk: @autoclosure @escaping () -> ObjectType) -
• https://github.com/yutailang0119/SBPAsyncImage/pull/14 ҎલɺҎԼ ͷΑ͏ʹStateObjectΛinit͍ͯͨ͠ @available(iOS 14.0, macOS 11.0, tvOS 14.0,
watchOS 7.0, *) private struct ContentBody<Content: View>: View { @StateObject private var viewModel: ViewModel private let content: (AsyncImagePhase) -> Content init(viewModel: ViewModel, @ViewBuilder content: @escaping (AsyncImagePhase) -> Content) { self._viewModel = .init(wrappedValue: viewModel) self.content = content } } Case2: StateObject Λinit͍͚ͯ͠ͳ͍ -
• @StateObject ͷinitViewʹͤͯɺStateObject͕ߦ͍͍ͨॲཧɺ onAppear(perform:) ʹҠಈ • ͘Θ͘͠ `Explanation behind the
error? “Accessing StateObject’s object without being installed on a View. This will create a new instance each time.”` • https://forums.swift.org/t/explanation-behind-the-error-accessing- stateobjects-object-without-being-installed-on-a-view-this-will- create-a-new-instance-each-time/40111 Case2: StateObject Λinit͍͚ͯ͠ͳ͍ -
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) private
struct ContentBody<Content: View>: View { @StateObject private var viewModel = ViewModel() ... var body: some View { content(viewModel.phase) .onAppear { viewModel.download(url: url, scale: scale, transaction: transaction) } } } Case2: StateObject Λinit͍͚ͯ͠ͳ͍ -
·ͱΊ • SwiftUI.AsyncImage ศརͰ͢Ͷ • ૈ͍Օॴ͋Δ • yutailang0119/SBPAsyncImage ΛΑΖ͘͠ •
SwiftUI׆༻ʹTrick͕ຬࡌ -
Reference • https://developer.apple.com/documentation/swiftui/asyncimage • https://github.com/yutailang0119/SBPAsyncImage • https://developer.apple.com/documentation/combine/observableobject • https://developer.apple.com/documentation/swiftui/stateobject •
https://kouki.hatenadiary.com/entry/2021/06/22/130000 • https://developer.apple.com/documentation/swiftui/stateobject/init(wrappedvalue:) • https://forums.swift.org/t/explanation-behind-the-error-accessing-stateobjects-object-without- being-installed-on-a-view-this-will-create-a-new-instance-each-time/40111