如果整个视图访问应用程序中的相同模型,我认为单例模式就足够了。我对吗?
例如,如果 MainView 和 ChildView 访问相同的模型(例如 AppSetting),如下所示,我找不到任何理由使用 EnvironmentObject 而不是 Singleton 模式。如果我这样使用有什么问题吗?如果可以,那么我什么时候应该使用 EnvironmentObject 而不是 Singleton 模式?
class AppSetting: ObservableObject {
static let shared = AppSetting()
private init() {}
@Published var userName: String = "StackOverflow"
}
Run Code Online (Sandbox Code Playgroud)
struct MainView: View {
@ObservedObject private var appSetting = AppSetting.shared
var body: some View {
Text(appSetting.userName)
}
}
Run Code Online (Sandbox Code Playgroud)
struct ChildView: View {
@ObservedObject private var appSetting = AppSetting.shared
var body: some View {
Text(appSetting.userName)
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢。
我无法弄清楚 compositingGroup() 是什么。起初,我认为它类似于Merging layersPhotoshop 中的东西。但事实并非如此。因为即使我使用 .compositingGroup(),.shadow() 也会分别对叠加视图和背景视图产生影响。
到目前为止,我在使用 .compositingGroup() 时发现了两个不同之处
compositingGroup 的目的是什么?
struct ContentView: View {
var body: some View {
VStack(spacing: 50) {
Text("Without\ncompositing")
.font(.largeTitle)
.bold()
.padding()
.foregroundColor(Color.white)
.background(RoundedRectangle(cornerRadius: 30).fill(Color.red))
.padding()
.padding()
.overlay(RoundedRectangle(cornerRadius: 30).stroke(lineWidth: 10))
.shadow(color: .blue, radius: 5)
Text("With\ncompositing")
.font(.largeTitle)
.bold()
.padding()
.foregroundColor(Color.white)
.background(RoundedRectangle(cornerRadius: 30).fill(Color.red))
.padding()
.padding()
.overlay(RoundedRectangle(cornerRadius: 30).stroke(lineWidth: 10))
.compositingGroup() // <--- I added .compositingGroup() here.
.shadow(color: .blue, radius: 5)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想用 Future 做一个异步工作。但是下面的.sink() 闭包永远不会被调用。看起来 Future 的实例在它被调用后就被释放了。
Future<Int, Never> { promise in
DispatchQueue.global().asyncAfter(deadline: .now() + 1) {
promise(.success(1))
}
}
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { completion in
print(completion)
}, receiveValue: {
print($0)
})
Run Code Online (Sandbox Code Playgroud)
所以我将.sink() 闭包替换为.subscribe(Subscribers.Sink()) ,如下所示。它工作正常。但问题是我不明白为什么它工作正常。:(对我来说看起来一样。这两个代码有什么区别?我什么时候可以使用.sink(),什么时候不能?
Future<Int, Never> { promise in
DispatchQueue.global().asyncAfter(deadline: .now() + 1) {
promise(.success(1))
}
}
.receive(on: DispatchQueue.main)
.subscribe(Subscribers.Sink(receiveCompletion: { completion in
print(completion)
}, receiveValue: {
print($0)
}))
Run Code Online (Sandbox Code Playgroud)
提前致谢。
我有3个问题.我一直试图在发布之前找到答案,但失败了.如果你知道的话,请留一个简短的评论.
问题#1
Chromecast现在支持MPEG-DASH吗?我已经测试了一个示例程序(cast-chrome,由google提供),通过将url替换为MEPG-DASH MPD URL来播放MPEG-DASH流.但设备无法播放流.我想知道MPEG-DASH现在可以在Developer Preview SDK中使用.或者我必须等到最终的sdk可用?
问题2
Chromecast是否支持MPEG-DASH的MPEG2-TS容器格式?
问题#3
WebM容器格式是否可用于MPEG-DASH?