小编fra*_*yan的帖子

Kotlin 状态流在具有视图模型范围的单元测试中不发出

我对 Kotlin 协程和流程以及它们的单元测试很陌生。我有一个非常简单的测试:

@Test
fun debounce(): Unit = runBlocking {
    val state = MutableStateFlow("hello")
    val debouncedState = state.debounce(500).stateIn(this, SharingStarted.Eagerly, "bla")

    assertThat(debouncedState.value).isEqualTo("bla")

    state.value = "good bye"

    // not yet...
    assertThat(debouncedState.value).isEqualTo("bla")

    delay(600)

    // now!
    assertThat(debouncedState.value).isEqualTo("good bye")

    // cannot close the state flows :(
    cancel("DONE")
}
Run Code Online (Sandbox Code Playgroud)

它工作得很好(除了我无法阻止它,但这是一个不同的问题)。

接下来,我想测试我的ViewModel,其中包括完全相同的状态流。它基本上与上面的代码相同,但我认为它应该在相同的范围内运行,所以viewModel.someMutableStateFlow我尝试运行它viewModelScope

@Test
fun debounce(): Unit = runBlocking {
    val viewModel = MyViewModel()

    // in view model the state and debouncedState are defined the same way as above
    val state = …
Run Code Online (Sandbox Code Playgroud)

kotlin kotlin-coroutines kotlin-flow

10
推荐指数
1
解决办法
1286
查看次数

iOS:AVPlayer - 获取视频当前帧的快照

我花了一整天时间,经历了很多SO答案,Apple参考文献,文档等,但都没有成功.

我想要一个简单的事情:我正在使用AVPlayer播放视频,我想暂停它并获取当前帧UIImage.而已.

我的视频是一个位于互联网上的m3u8文件,它在AVPlayerLayer没有任何问题的情况下正常播放.

我试过了什么:

  1. AVAssetImageGenerator.它不起作用,该方法copyCGImageAtTime:actualTime: error:返回null image ref.根据答案,此处 AVAssetImageGenerator不适用于流媒体视频.
  2. 拍摄玩家视图的快照.我第一次尝试renderInContext:AVPlayerLayer,但后来我意识到,这不是渲染这种"特殊"层.然后我发现了iOS 7中引入的一种新方法 - drawViewHierarchyInRect:afterScreenUpdates:它应该能够渲染特殊的图层,但没有运气,仍然可以获得带有空白黑色区域的UI快照,其中显示视频.
  3. AVPlayerItemVideoOutput.我为我添加了一个视频输出AVPlayerItem,但每当我调用hasNewPixelBufferForItemTime:它时返回NO.我想问题是再次流式传输视频,我并不孤单.
  4. AVAssetReader.我正在考虑尝试,但决定在这里找到相关问题后不要浪费时间.

所以,有没有办法得到我现在在屏幕上看到的东西的快照?我简直不敢相信.

video objective-c avfoundation ios avplayer

9
推荐指数
2
解决办法
6941
查看次数

Swift Generics:无法将类型的值转换为预期的参数类型

这是我的代码:

protocol SomeProtocol {
}

class A: SomeProtocol {
}

func f1<T: SomeProtocol>(ofType: T.Type, listener: (T?) -> Void) {
}

func f2<T: SomeProtocol>(ofType: T.Type, listener: ([T]?) -> Void) {
}

func g() {
    let l1: (SomeProtocol?) -> Void = ...
    let l2: ([SomeProtocol]?) -> Void = ...
    f1(ofType: A.self, listener: l1) // NO ERROR
    f2(ofType: A.self, listener: l2) // COMPILE ERROR: Cannot convert value of type '([SomeProtocol]?) -> Void' to expected argument type '([_]?) -> Void'
}
Run Code Online (Sandbox Code Playgroud)

第二个闭包有一个泛型类型对象数组的参数有什么问题?

generics swift

9
推荐指数
1
解决办法
5383
查看次数

对象适合的图像圆角问题:包含

我想显示带有圆角的图像。因此,图像必须拉伸到容器,但不会裁剪任何部分,例如object-fit: contain. 但是,border-radius适用于图像元素,而不是图片内容。这是一个示例(也是JSFiddle):

body {
  width: 100vw;
  height: 100vh;
  margin: 0;
}

div {
  width: 100%;
  height: 100%;
}

img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 20%;
}
Run Code Online (Sandbox Code Playgroud)
<div>
  <img src="http://imgs.ntdtv.com/pic/2018/1-23/p8651401a412676208.jpg">
</div>
Run Code Online (Sandbox Code Playgroud)

您可以在调整视口大小时检查它是如何工作的。

那么,有没有办法让图像元素在两个方向上调整其边框的大小以适应容器,就像object-fit那样?

或者也许是一种在图像内容上应用“裁剪四舍五入的矩形过滤器”的方法?

html css

9
推荐指数
2
解决办法
6309
查看次数

从 Android 中的挂起函数并行调用 Kotlin 协程

我对协程相对较新,所以我想知道如何解决我的本地小问题而不需要大量重组我的 Android 代码。

这是一个简单的设置。我的 ViewModel 从存储库调用一个suspend函数:

// ...ViewModel.kt

fun loadData() {
    viewModelScope.launch {
        val data = dataRepository.loadData()
    }
}
Run Code Online (Sandbox Code Playgroud)

这非常方便,因为我已经viewModelScope为 Android 准备好了一个,并且我从我的存储库中调用了一个挂起函数。我不关心存储库如何加载数据,我只是暂停直到它返回给我。

我的数据存储库使用以下命令进行多次调用Retrofit

//...DataRepository.kt

@MainThread
suspend fun loadData(): ... {
    // Retrofit switches the contexts for me, just
    // calling `suspend fun getItems()` here.
    val items = retrofitApi.getItems()
    val itemIDs = items.map { it.id }

    // Next, getting overall list of subItems for each item. Again, each call and context
    // switch for `suspend …
Run Code Online (Sandbox Code Playgroud)

android kotlin kotlin-coroutines

8
推荐指数
1
解决办法
1万
查看次数

检查Swift对象是否是给定元类型的实例

我需要保留Swift元类型的集合并编写一个函数,该函数将检查给定对象是否是其中一个的实例.我可以在Java中轻松完成:

Class c = x.getClass();
c.isInstance(someObj)
Run Code Online (Sandbox Code Playgroud)

但是,我不知道如何在Swift中这样做:

var isInt = 7 is Int.Type // compiles

let x = Int.self
var isInt = 7 is x // compiler error - Use of undeclared type 'x'
Run Code Online (Sandbox Code Playgroud)

这甚至可以在Swift中完成吗?

types swift metatype

7
推荐指数
1
解决办法
1381
查看次数

EncryptedSharedPreferences 导致 UnrecoverableKeyException

我正在以EncryptedSharedPreferencesGoogle 推荐的方式使用新类:

private fun securePrefs(context: Context): SharedPreferences {
    val fileName = "sharedPrefsSecure"
    val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
    return EncryptedSharedPreferences.create(fileName, masterKeyAlias, context,
        EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM)
}
Run Code Online (Sandbox Code Playgroud)

但是,该应用程序在第二天在模拟器(Pixel 2 API 26)上崩溃了。每次启动应用程序时都会发生这种情况,直到我完全卸载它并再次重新安装(从带有运行的 Studio) - 然后它工作正常。但是,一旦我重新启动模拟器,它就会再次崩溃,直到完全重新安装。即使在重新启动后,我的三星也很好。我想知道我是否也会在真实设备上得到类似的东西。

这是崩溃堆栈跟踪:

2020-02-24 17:42:52.632 12398-12398/com.myapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.myapp, PID: 12398
    java.security.UnrecoverableKeyException: Failed to obtain information about key
        at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreSecretKeyFromKeystore(AndroidKeyStoreProvider.java:282)
        at android.security.keystore.AndroidKeyStoreSpi.engineGetKey(AndroidKeyStoreSpi.java:98)
        at java.security.KeyStore.getKey(KeyStore.java:1062)
        at com.google.crypto.tink.integration.android.AndroidKeystoreAesGcm.<init>(AndroidKeystoreAesGcm.java:48)
        at com.google.crypto.tink.integration.android.AndroidKeystoreKmsClient.getAead(AndroidKeystoreKmsClient.java:111)
        at com.google.crypto.tink.integration.android.AndroidKeystoreKmsClient.getOrGenerateNewAeadKey(AndroidKeystoreKmsClient.java:130)
        at com.google.crypto.tink.integration.android.AndroidKeysetManager.<init>(AndroidKeysetManager.java:118)
        at com.google.crypto.tink.integration.android.AndroidKeysetManager.<init>(AndroidKeysetManager.java:88)
        at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.build(AndroidKeysetManager.java:185)
        at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:123)
        at com.myapp.base.service.DefaultKeystoreService.securePrefs(DefaultKeystoreService.kt:43)
        at com.myapp.base.service.DefaultKeystoreService.sharedPrefsForKey(DefaultKeystoreService.kt:37)
        at com.myapp.base.service.DefaultKeystoreService.get(DefaultKeystoreService.kt:17)
        at com.myapp.base.repository.DefaultCredentialsRepository.getRefreshToken(DefaultCredentialsRepository.kt:78)
        at …
Run Code Online (Sandbox Code Playgroud)

android android-keystore androidx androidx-security

7
推荐指数
0
解决办法
1656
查看次数

在 QMessageBox 中添加详细文本会禁用关闭 (X) 按钮

我注意到一件有趣的事情 - 如果我向 QMessageBox 添加详细文本(它添加了“显示详细信息...”按钮),然后执行它将显示系统框架的关闭 (X) 按钮已禁用,因此将此窗口标记为不可关闭(右键单击框架 -> 关闭禁用)。

下面是一些示例代码:

QMessageBox box(QMessageBox::Critical, title, text, QMessageBox::Ok);
box.setDetailedText(detailedText); // comment this line to get close button enabled
box.exec();
Run Code Online (Sandbox Code Playgroud)

我什至没有找到在 Qt 中手动执行此操作的方法。有任何想法吗?

谢谢

qt qmessagebox

6
推荐指数
1
解决办法
6389
查看次数

使用UIAppearance更改所有UITableViewCell的文本颜色

我无法更改UITableViewCell使用UIAppearance机制的文本颜色.

这是我的代码,其中的注释显示了什么对我有用,什么不对:

UITableViewCell *cell = [UITableViewCell appearance];
cell.backgroundColor = [UIColor blueColor]; // working
cell.textLabel.textColor = [UIColor whiteColor]; // NOT WORKING
cell.detailTextLabel.textColor = [UIColor redColor]; // NOT WORKING

UILabel *cellLabel = [UILabel appearanceWhenContainedIn:[UITableViewCell class], nil];
cellLabel.textColor = [UIColor whiteColor]; // working
Run Code Online (Sandbox Code Playgroud)

如您所见,第二种方式是工作,但我不能为普通文本和详细文本设置不同的颜色.

有什么我做错了吗?

PS在Interface Builder中静态定义东西对我来说不起作用 - 我有可以在运行时动态更改的主题.

objective-c uitableview ios uiappearance

6
推荐指数
1
解决办法
2084
查看次数

在 iOS 13 中,UITabBarItem 的 standardAppearance 应用于所有其他项目

我正在尝试在 iOS 13 上做一件简单的事情 -最后一个选项卡栏项目应始终以不同的颜色显示

我尝试使用新UITabBarItem.standardAppearance成员。这是我的代码:

// first, set the default colors for the whole tab bar
let color = Style.Color.tabItem
let text = [NSAttributedString.Key.foregroundColor: color]
let selectedColor = Style.Color.tabItemSelected
let selectedText = [NSAttributedString.Key.foregroundColor: selectedColor]

let barAppearance = UITabBarItemAppearance()

barAppearance.normal.iconColor = color
barAppearance.disabled.iconColor = color
barAppearance.selected.iconColor = selectedColor
barAppearance.focused.iconColor = selectedColor

barAppearance.normal.titleTextAttributes = text
barAppearance.disabled.titleTextAttributes = text
barAppearance.selected.titleTextAttributes = selectedText
barAppearance.focused.titleTextAttributes = selectedText

tabBar.standardAppearance.stackedLayoutAppearance = barAppearance
tabBar.standardAppearance.inlineLayoutAppearance = barAppearance
tabBar.standardAppearance.compactInlineLayoutAppearance = barAppearance
tabBar.standardAppearance.backgroundColor = Style.Color.tabBar

// …
Run Code Online (Sandbox Code Playgroud)

uitabbaritem uitabbar ios

6
推荐指数
1
解决办法
3090
查看次数