遵循firestore的官方文档:
{
name: "Frank",
favorites: { food: "Pizza", color: "Blue", subject: "recess" },
age: 12
}
// To update favorite color:
db.collection("users").doc("frank").update({
"favorites.color": "Red"
})
Run Code Online (Sandbox Code Playgroud)
我想使用动态键而不是颜色.
db.collection("users").doc("frank").update({
"favorites[" + KEY + "].color": true
});
Run Code Online (Sandbox Code Playgroud)
这当然是不可能的,并会抛出错误.
我一直试图这样做:
db.collection("users").doc("frank").update({
favorites: {
[key]: {
color": true
}
}
});
Run Code Online (Sandbox Code Playgroud)
它实际上是使用正确的密钥更新,但遗憾的是,它正在覆盖其他密钥(它们正被删除).
我正在努力了解DispatchSourceTimer,Timer和asyncAfter之间的主要区别(在我的情况下,该任务需要每X秒运行一次,尽管了解计时器的区别可能对有用)(或者是否有另一个(更有效)中除了列出的计时器外,还可以在Swift中使用调度机制?)。
A Timer需要在其开始的当前队列上有一个活动的运行循环。一个DispatchSourceTimer不需要。A Timer可防止CPU进入空闲状态。这是否也适用于DispatchSourceTimer/ asyncAfter?
在什么情况下,a Timer优于DispatchSourceTimer/ asyncAfter?当然,它们之间的区别是什么?
我想在私人队列中的应用程序中每15秒安排一次工作。这意味着我必须使用,DispatchSourceTimer因为我在不是主线程的队列中(或将runloop添加到队列中并使用Timer)。但是,即使Timer最初使用a,我也看不到任何好处。也许我可以使用另一个操作在私有队列上每X秒执行一次调度工作,该工作比a更为有效DispatchSourceTimer,但是我没有遇到更好的解决方案。
是一个DispatchSourceTimer比一个更有效Timer?还是应该继续使用的自调用方法asyncAfter?
这是创建计时器的代码。
异步之后
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(2)) {
// Code
}
Run Code Online (Sandbox Code Playgroud)
计时器
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { (_) in
// Code
}
Run Code Online (Sandbox Code Playgroud)
DispatchSourceTimer
let timer = DispatchSource.makeTimerSource()
timer.schedule(deadline: .now() + .seconds(1))
timer.setEventHandler {
// Code
}
timer.activate()
Run Code Online (Sandbox Code Playgroud)
所有计时器的利弊是什么?什么时候应该在另一个之上使用?哪种计时器方式最有效?我想出了以下几点: …
We can test thrown errors with XCTAssertThrowsError. Async things can be tested with expectation. I have some method which dispatch work to a background thread and can at some point throw an error.
Is it possible to expect an error be thrown somewhere in the future? I need to combine expectation and XCTAssertThrowsError I think, but I do not know how.
复制项目: https: //github.com/Jasperav/ThrowingAsyncError。只需克隆项目并运行测试,其中之一就会失败。我创建了一个类,该类在分配后几秒钟后就会崩溃。我想确保它在几秒钟后继续崩溃,所以我想要一个测试用例。
我CoreData用a NSFetchResultController来显示数据UITableView.我有一个问题:UITableView更改contentOffSet.y插入/移动/删除新行的时间.当用户滚动到例如中间时,UITableView当插入新行时弹跳.
这个github链接到一个项目,其中包含重现此行为的最小代码:https://github.com/Jasperav/FetchResultControllerGlitch(代码也在下面)
这显示了故障.我站在我的中间,我UITableView不断看到插入新行,无论当前contentOffSet.y.
当NSFetchedResultsController添加新记录时,如何阻止向上滚动UITableView?
不相关,因为我明确设置rowHeight和
estimatedRowHeight.
错误:UITableView跳转到顶部,UITableViewAutomaticDimension
在endUpdates没有运气之前尝试了这个
UITableView由带有UITableViewAutomaticDimension的FetchedResultsController提供 - 当重新加载表时单元格移动
与第一个链接相同,我设置了rowHeight和
estimatedRowHeight.
我也试过切换到,performBatchUpdates而不是begin/endUpdates,也没有解决.
在UITableView插入时/删除/移动行时只是应保持不动那些行不可见的用户.我希望这样的东西应该开箱即用.
这是我最终想要的(只是复制WhatsApp的聊天屏幕):
UITableView应该为新插入的行设置动画并更改当前contentOffSet.y.如果a UICollectionView更合适,那就没问题了.
我想复制WhatsApp聊天屏幕.我不确定他们是否使用NSFetchResultController,但除此之外,最终目标是为他们提供确切的用户体验.因此,插入,移动,删除和更新单元格应该按照WhatsApp的方式进行.因此,对于一个工作示例:转到WhatsApp,获取一个不工作的示例:下载项目.
代码(ViewController.swift):
import CoreData
import UIKit
class ViewController: UIViewController, NSFetchedResultsControllerDelegate, …Run Code Online (Sandbox Code Playgroud) 该DynamoDB维基百科的文章说,DynamoDB是一个“键-值”的数据库。然而,将其称为“键值”数据库完全忽略了 DynamoDB 的一个极其基本的特性,即排序键:键有两部分(分区键和排序键),并且可以有效地检索到具有相同分区键的项目一起排序通过排序键。
Cassandra 还具有完全相同的 sort-items-inside-a-partition 功能(它称为“集群键”),并且Cassandra 维基百科文章使用术语宽列存储来描述它。然而,虽然这个术语“宽列”比“键值”要好,但它仍然有些不合适,因为它描述了更一般的情况,即一个项目可以有大量不相关的列——不一定是单独的排序列表项目。
所以我的问题是是否有一个更合适的术语可以描述像 DynamoDB 和 Cassandra 这样的数据库的数据模型——像键值存储这样的数据库可以有效地检索单个键的项目,但也可以有效地检索按键或只是其中的一部分(DynamoDB 的排序键或 Cassandra 的集群键)。
编辑
有关最新项目,请参阅Nathan的评论部分.只剩下问题:获得正确的按钮.
编辑
我想要一个用户可以旋转的UIView.UIView应包含一些可以单击的UIButton.我很难,因为我使用UIControl子类来制作旋转视图,在该子类中,我必须禁用UIControl中子视图的用户交互(使其旋转),这可能导致UIButton无法点击.如何创建用户可以旋转并包含可点击的UIButton 的UIView ?这是我项目的链接,它为您提供了一个良好的开端:它包含UIButtons和可旋转的UIView.但是我可以不点击UIButtons.
老问题有更多细节
我正在使用这个pod:https://github.com/joshdhenry/SpinWheelControl,我想对按钮点击做出反应.我可以添加按钮,但是我无法在按钮中接收点击事件.我正在使用hitTests,但它们永远不会被执行.用户应旋转滚轮,并能够单击其中一个饼图中的按钮.
在这里获取项目:https://github.com/Jasperav/SpinningWheelWithTappableButtons
请参阅下面的代码我在pod文件中添加的内容:
我在SpinWheelWedge.swift中添加了这个变量:
let button = SpinWheelWedgeButton()
Run Code Online (Sandbox Code Playgroud)
我添加了这个类:
class SpinWheelWedgeButton: TornadoButton {
public func configureWedgeButton(index: UInt, width: CGFloat, position: CGPoint, radiansPerWedge: Radians) {
self.frame = CGRect(x: 0, y: 0, width: width, height: 30)
self.layer.anchorPoint = CGPoint(x: 1.1, y: 0.5)
self.layer.position = position
self.transform = CGAffineTransform(rotationAngle: radiansPerWedge * CGFloat(index) + CGFloat.pi + (radiansPerWedge / 2))
self.backgroundColor = .green
self.addTarget(self, action: #selector(pressed(_:)), for: …Run Code Online (Sandbox Code Playgroud) 我很好奇为什么这不起作用:
public protocol MyProtocol {
var i: Int { get set }
}
public protocol MyProtocol2: class, MyProtocol {}
public extension MyProtocol2 where Self: AnyObject {
func a() {
i = 0 <-- error
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
无法分配属性:'self'是不可变的
为什么?只有类可以采用MyProtocol2.如果我: class在MyProtocol后面添加声明它可以工作.我不明白为什么它不适用于子协议.
我的目标是创建一个slotmachine,其中行是一个接一个地旋转,他们需要一个接一个地停止旋转.然而,为了使它看起来不错,行需要至少旋转3秒.我认为PickerView是最好的选择,因为我不知道如何以不同的方式使这项工作.
这是我的代码:
self.slotMachine.selectRow(99, inComponent: 1, animated: true)
Run Code Online (Sandbox Code Playgroud)
PickerView将转到第99行,但是在1秒内.如何控制第二个(并扩展选择行过程)?一个条件是它应该看起来不错,感觉就像你在玩老虎机.我试过这个:
UIView.animate(withDuration: 3.0, delay: 0, animations: { () -> Void in
self.slotMachine.selectRow(99, inComponent: 1, animated: true)
}, completion: nil )
Run Code Online (Sandbox Code Playgroud)
但这没效果.
谢谢.
我在我的文档中得到了这些数据:
我想删除索引0.我该怎么做?这应该是我想到的技巧:
db.collection("data").document("free").updateData(["deleteme.deletemee.0" : FieldValue.delete()]) { (errr) in
print(errr)
}
Run Code Online (Sandbox Code Playgroud)
但是errr打印为nil,没有删除任何内容.在获取文档时,我注意到使用此代码时数据有些奇怪:
db.collection("data").document("free").getDocument { (doc, err) in
guard let _doc = doc,
doc?.exists ?? false else{ return }
print(_doc.data())
}
Run Code Online (Sandbox Code Playgroud)
打印出:
["deleteme": {
deletemee = (
1 //this is the value for the key, but where is my key?! :(
);
}]
Run Code Online (Sandbox Code Playgroud)
我看不出钥匙,它在哪里?如何在Firestore中删除索引处的内容?谢谢.
swift ×6
ios ×3
firebase ×2
animation ×1
cassandra ×1
core-data ×1
hittest ×1
javascript ×1
protocols ×1
scylla ×1
timer ×1
uicontrol ×1
uipickerview ×1
uitableview ×1
unit-testing ×1
xcode ×1
xcode9 ×1
xctest ×1