如何控制和平衡我的应用程序正在执行的线程数,如何限制其数量以避免应用程序阻塞,因为达到了线程限制?
在SO上我看到了以下可能的答案:"主并发队列(dispatch_get_global_queue)自动管理线程数",由于以下原因,我不喜欢这样做:
考虑以下模式(在我的真实应用程序中,有更简单和更复杂的示例):
dispatch_queue_t defaultBackgroundQueue() {
return dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
}
dispatch_queue_t databaseQueue() {
dispatch_queue_create("Database private queue", 0);
}
dispatch_async(defaultBackgroundQueue(), ^{
[AFNetworkingAsynchronousRequestWithCompletionHandler:^(data){
dispatch_async(databaseQueue(), ^{
// data is about 100-200 elements to parse
for (el in data) {
}
maybe more AFNetworking requests and/or processing in other queues or
dispatch_async(dispatch_get_main_queue(), ^{
// At last! We can do something on UI.
});
});
}];
});
Run Code Online (Sandbox Code Playgroud)
这种设计经常导致以下情况:
明显而愚蠢的解决方案是用dispatch_sync替换敏感的dispatch_async方法,但绝对是我不喜欢的方法.
这种情况的推荐方法是什么?
我希望答案比"使用NSOperationQueue - 它可以限制并发操作的数量"更加智能(类似主题:具有NSOperationQueueDefaultMaxConcurrentOperationCount的线程数).
更新1:唯一可行的模式是:将所有dispatch_async的块替换为并发队列,并在NSOperationQueue基于NSOperationQueue的并发队列中运行这些块,并设置最大操作限制(在我的情况下,也可能设置最大操作限制) AFNetworking运行其所有操作的基于NSOperationQueue的队列.
当应用程序变为活动状态时,我正在使用UIApplicationDidBecomeActiveNotification刷新我的tableview.我的问题是,在我的ViewWillAppear中,我还调用了一个刷新该表数据的方法.
这导致在应用程序启动时刷新表两次.在应用程序最初启动时,如何让其中一个不要触发?刷新表有一些密集的网络和本地数据处理..所以我真的希望o只执行一次此操作.
谢谢.
我使用MKAnnotationView的自定义子类.在mapView:didSelectAnnotationView:我的Map委托的方法我调用这个类的方法,它将UIImageView与图像作为子视图添加 - 它作为我的自定义注释标注.
当使用默认的MKPinAnnotationView地图时,会自动调整地图区域以显示刚刚出现的注释标注.如何使用自定义MKAnnotationView子类实现此行为?
我有以下代码:
- (void)test_with_running_runLoop {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSTimeInterval checkEveryInterval = 0.05;
NSLog(@"Is main queue? : %d", dispatch_get_current_queue() == dispatch_get_main_queue());
dispatch_async(dispatch_get_main_queue(), ^{
sleep(1);
NSLog(@"I will reach here, because currentRunLoop is run");
dispatch_semaphore_signal(semaphore);
});
while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:checkEveryInterval]];
NSLog(@"I will see this, after dispatch_semaphore_signal is called");
}
- (void)test_without_running_runLoop {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSLog(@"Is main queue? : %d", dispatch_get_current_queue() == dispatch_get_main_queue());
dispatch_async(dispatch_get_main_queue(), ^{
sleep(1);
NSLog(@"I will not reach here, because currentRunLoop is not run");
dispatch_semaphore_signal(semaphore);
}); …Run Code Online (Sandbox Code Playgroud) 我尝试使用swift向状态栏添加一个简单的状态菜单,但不会显示.
与Objective-c一起工作:
AppDelegate.h
@interface AppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSMenu *statusMenu;
NSStatusItem * statusItem;
}
@end
Run Code Online (Sandbox Code Playgroud)
AppDelegate.m
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem setMenu:statusMenu];
[statusItem setTitle:@"Status Menu"];
[statusItem setHighlightMode:YES];
}
@end
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试在swift中基本上做同样的事情,它什么都不做.
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet var statusMenu: NSMenu;
func applicationDidFinishLaunching(aNotification: NSNotification?) {
let bar = NSStatusBar.systemStatusBar()
let statusItem = bar.statusItemWithLength(CGFloat(NSVariableStatusItemLength))
statusItem.title = "Status Menu"
statusItem.menu = statusMenu
statusItem.highlightMode = true
}
}
Run Code Online (Sandbox Code Playgroud)
没有错误,它只是没有做任何事情.函数applicationDidFinishLaunching被调用,因为它内部的println()创建输出.
有谁知道我在这里做错了什么?
我在 Documents 文件夹中创建了一个名为“Photos”的文件夹。
使用 Swift 代码,我想打印照片文件夹中文件的名称。
我想强制Xcode使用自定义编译器(从src构建'clang-llvm'),以便可以使用clang插件。我的Xcode版本是7.3.1。
我正在寻找一种正确的方法来获得遵循给定指令的下一个立即指令.
我们假设我有以下内容:
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %17
Run Code Online (Sandbox Code Playgroud)
我有一个
CmpInst *cmpInst = dyn_cast<CmpInst>(&V);
Run Code Online (Sandbox Code Playgroud)
对应于%10.
我怎样才能访问BranchInst我的CmpInst?
我假设一个解决方案应该考虑两种情况:当有下一条指令而没有一条指令时,即它是a的结尾BasicBlock.
我有mylib.c一些有一些功能的文件.我想.c在编译的llvm代码中使用我的文件中的那些函数作为外部函数.我正在玩LLVM解释器(lli-4.0),我想知道如何lli使用我的.c文件中的函数?
我在一个大型iOS应用程序项目中有许多仅Swift框架的目标。我想探索Swift编译器选项的严格性,以了解它们是否可以带来任何其他好处。
Xcode中有许多警告,但大多数警告似乎与Clang有关。我们可以对Swift编译器执行的等效-Wall或等效操作是什么-Weverything?
一个示例是警告,以防止局部变量出现阴影Apple LLVM 9.0 - Warnings - All languages / Hidden local variables::它位于GCC_WARN_SHADOW = YES幕后,因此不会影响swiftc编译器。