我无法弄清楚如何在Swift 3.0中重复调度计时器.我的代码:
let queue = DispatchQueue(label: "com.firm.app.timer",
attributes: DispatchQueue.Attributes.concurrent)
let timer = DispatchSource.makeTimerSource(flags: DispatchSource.TimerFlags(rawValue: UInt(0)),
queue: queue)
timer.scheduleRepeating(deadline: DispatchTime.now(),
interval: .seconds(5),
leeway: .seconds(1)
)
timer.setEventHandler(handler: {
//a bunch of code here
})
timer.resume()
Run Code Online (Sandbox Code Playgroud)
计时器只会触发一次并且不会像它应该的那样重复.我怎样才能解决这个问题?
我正在尝试执行一系列网络请求,并希望限制新的 Swift 并发系统中的并发任务数量。对于操作队列,我们将使用maxConcurrentOperationCount. 在组合中,flatMap(maxPublishers:_:). 新的 Swift Concurrency 系统中的等价物是什么?
例如,它并不是非常相关,但请考虑:
func downloadAll() async throws {
try await withThrowingTaskGroup(of: Void.self) { group in
for index in 0..<20 {
group.addTask { try await self.download(index) }
}
try await group.waitForAll()
}
}
Run Code Online (Sandbox Code Playgroud)
这会导致所有请求同时运行:
URLSession不尊重这一事实httpMaximumConnectionsPerHost很有趣,但这不是这里的突出问题。更一般地说,我正在寻找如何限制一系列并行运行的异步任务的并发程度。
我正在尝试自定义我的UINavigationBar字体,在我的app delegate中使用以下iOS 5代码application:didFinishLaunchingWithOptions:
if ([[UINavigationBar class] respondsToSelector:@selector(appearance)])
{
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(1, 0)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:kDefaultFont size:0.0], UITextAttributeFont,
nil]];
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,导航栏使用我的字体呈现.大.
我发现的参考文献建议您可以使用零字体大小,它将调整字体大小以适合您的导航栏(使用稍小的字体用于横向布局的较短导航栏).它确实选择了适合导航栏高度的字体大小.但看起来如果你从纵向转向横向和背面,导航栏的标题标签的宽度会被搞砸,所以当你第一次看到它时,显示为"Long Titlebar"的标题看起来很好在纵向方向,当你在横向中查看时看起来很好(使用相应较小的字体),但当我回到肖像时,字体正确地恢复为较大的字体,但标题文本本身被截断,变为"长... "尽管有足够的空间可以获得完整的冠军头衔.当使用0.0的字体大小时,是否有其他人看到过这种行为?
很显然,我可以指定实际的字体大小(在这种情况下,我不认为这截断行为),但后来我手动搞清楚使用什么尺寸.更糟糕的是,横向和纵向的字体大小相同,所以现在我使用的字体大小适合较短的横向导航栏标题,并且标题小于它需要在更高的纵向导航栏中.
有人在那里有使用setTitleTextAttributes改变字体的经验[UINavigationBar appearance],使得字体大小在纵向和横向之间变化,但是当你去景观后返回肖像时没有标题的截断吗?我即将寻求各种麻烦的解决方法,但如果您对此问题有任何经验,请告诉我.
更新:
在向Apple提交此错误的过程中,我决定演示如何重现该问题:
在Xcode 4.3.2中创建新的iOS Master-Detail应用程序.
将上面的setTitleTextAttributes代码放在app delegate中application:didFinishLaunchingWithOptions(我使用了字体@"GillSans").
转到MasterViewController并添加说明的行 self.title = @"Long Title";
注释掉UIBarButtonItem *addButton代码.
运行程序.注意标题正确地说"长标题".旋转到风景.看起来还不错.旋转回肖像,标题现在显示"长......",即使有足够的空间.
奇怪的是,如果您恢复UIBarButtonItem *addButton代码,标题将按预期工作.但是,如果您要么取消UIBarButton项目,或者使用initWithTitle而不是使用的按钮替换它,则initWithBarButtonSystemItem在从纵向旋转到横向然后再回到纵向之后,您会遇到导航栏标题的问题.
什么是iOS中多线程的最佳方式,因为我们有三个选项GCD NSThread,和NSOperationQueue?我很困惑哪一个是最好的?如果没有,那么哪个应该用于什么情况以及它们如何不同,如果有人有一些很好的使用示例NSOperationQueue,请分享以便我可以学习.
multithreading nsthread nsoperationqueue grand-central-dispatch ios
我在Android项目中使用推送通知(GCM).
根据GCM教程,我实现了广播接收器并将其注册AndroidManifest.xml.
即使我的应用程序已关闭,这种广播接收器也应该接收消息(不仅是我的应用程序在后台,但即使它被强制停止).
但它没有像我期望的那样起作用.onReceive()如果应用关闭,则不会调用方法.似乎我对广播接收器的理解是正确的,问题在于我对GCM的期望.
其中一个可能的原因是,如果应用关闭,Google服务器甚至不会向设备发送通知.那么,只有当我的应用程序onReceive()正在运行或在后台(但未关闭)时,我的应用程序才能接收消息(并且方法将在广播接收器中调用),这是否正确?
提前致谢.
android broadcastreceiver push-notification google-cloud-messaging
我在我的UITableView中使用自定义UITableViewCell,但问题是当调用dequeueReusableCellWithIdentifier时,单元格永远不会为nil.为什么是这样 ?
- (void)viewDidLoad
{
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:@"PHResultTableViewCell" bundle: nil];
[[self tableView] registerNib:nib forCellReuseIdentifier:@"MyCell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PHResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
if (cell == nil)
{
PackageHolidayItem *obj=[[PackageHolidayItem alloc]init];
obj= [totalArray objectAtIndex:indexPath.row];
cell.packageHolidayItem = obj;
[cell loadRow];
}
return cell;
}
Run Code Online (Sandbox Code Playgroud) 曾经有一个很好的工具,DTSendSignalFlag它是DTPerformanceSession框架的一部分,通过它可以以编程方式将标志插入到仪器中(参见Xcode Instruments跟踪比较).此功能在iOS 7中停止运行.
有没有人成功地DTSendSignalFlag在iOS 7中工作?信号标志是(有?)一种有用的方式,可以通过代码以编程方式在仪器中发布标志(在诊断仪器中的复杂应用程序时非常有用),但是当我在iOS 7模拟器上运行时,我没有看到我在程序中创建的标志.但是当我为iOS 6模拟器安装Xcode 5时,它可以工作.
我试图开始Operation在侧面项目中使用s而不是在我的网络代码中散布基于闭包的回调以帮助消除嵌套调用.所以我正在做一些关于这个主题的阅读,我遇到了这个实现:
open class AsynchronousOperation: Operation {
// MARK: - Properties
private let stateQueue = DispatchQueue(label: "asynchronous.operation.state", attributes: .concurrent)
private var rawState = OperationState.ready
private dynamic var state: OperationState {
get {
return stateQueue.sync(execute: {
rawState
})
}
set {
willChangeValue(forKey: "state")
stateQueue.sync(flags: .barrier, execute: {
rawState = newValue
})
didChangeValue(forKey: "state")
}
}
public final override var isReady: Bool {
return state == .ready && super.isReady
}
public final override var isExecuting: Bool {
return state …Run Code Online (Sandbox Code Playgroud) 我在 Swift 5.5 中遇到了问题,但我不太明白解决方案。
import Foundation
func testAsync() async {
var animal = "Dog"
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
animal = "Cat"
print(animal)
}
print(animal)
}
Task {
await testAsync()
}
Run Code Online (Sandbox Code Playgroud)
这段代码会导致错误
Mutation of captured var 'animal' in concurrently-executing code
Run Code Online (Sandbox Code Playgroud)
但是,如果将animal变量移离此异步函数的上下文,
import Foundation
var animal = "Dog"
func testAsync() async {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
animal = "Cat"
print(animal)
}
print(animal)
}
Task {
await testAsync()
}
Run Code Online (Sandbox Code Playgroud)
它会编译。我知道这个错误是为了防止数据竞争,但为什么移动变量会使其安全?
我正在使用一个交互式自定义推送转换UIPercentDrivenInteractiveTransition.手势识别器成功调用交互控制器updateInteractiveTransition.同样,当我调用交互控制器时动画成功完成finishInteractiveTransition.
但是,有时我会在结尾处获得额外的一些分散注意力的动画(它似乎重复了动画的后半部分).有了相当简单的动画,我很少在iPhone 5上看到这种症状(虽然我在慢速笔记本电脑上工作时经常在模拟器上看到它).如果我使动画的计算成本更高(例如,大量阴影,多个视图为不同方向设置动画等),则设备上此问题的频率会增加.
有没有其他人看到这个问题,并找出一个解决方案,除了简化动画(我承认应该做的)和/或编写我自己的交互控制器?这种UIPercentDrivenInteractiveTransition方法有一定的优雅,但我对它不确定地行为不端的事实感到不安.别人看到过这种行为吗?有谁知道其他解决方案?
要说明效果,请参见下图.注意第二个场景,红色视图,当动画结束时,似乎第二次重复动画的后半部分.

此动画由以下内容生成:
反复呼叫updateInteractiveTransition,进度从0%升级到40%;
暂时停顿(因此您可以区分交互式过渡和完成动画finishInteractiveTransition);
然后调用finishInteractiveTransition完成动画; 和
动画控制动画的completion块调用completeTransition的transitionContext,以清理一切.
做一些诊断,似乎是最后一步触发了无关的动画.动画完成时会调用动画控制器的完成块,但是一旦我调用completeTransition它,它有时会重复动画的最后一位(特别是在使用复杂的动画时).
我不认为它是相关的,但这是我的配置导航控制器以执行交互式转换的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.delegate = self;
self.interationController = [[UIPercentDrivenInteractiveTransition alloc] init];
}
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController*)fromVC
toViewController:(UIViewController*)toVC
{
if (operation == UINavigationControllerOperationPush)
return [[PushAnimator alloc] init];
return nil;
}
- (id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController*)navigationController
interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>)animationController
{
return self.interationController;
}
Run Code Online (Sandbox Code Playgroud)
我PushAnimator是:
@implementation …Run Code Online (Sandbox Code Playgroud) ios ×5
swift ×4
async-await ×2
android ×1
dispatch ×1
dtsignalflag ×1
ios5 ×1
ios7 ×1
nsoperation ×1
nsthread ×1
swift3 ×1
uitableview ×1
xcode14 ×1