我的应用程序中有2个线程,一个游戏更新线程和渲染/ IO /主线程.我的更新线程更新游戏状态,渲染线程根据游戏状态模型的更新值和存储在对象(gameEngine)中的一些其他变量来渲染场景.
渲染线程在游戏线程仍在更新时执行,这是一个问题,因此在我看来,解决方案是使用@synchronized,如下所示:
@synchronized(gameEngine)
{
[gameEngine update];
nextUpdate = now + GAME_UPDATE_INTERVAL;
gameEngine.lastGameUpdateInterval = now - lastUpdate;
gameEngine.lastGameUpdateTime = now;
lastUpdate = now;
}
Run Code Online (Sandbox Code Playgroud)
但渲染线程仍然访问块之间-update的最后3行之间的gameEngine对象.为什么是这样?
我的问题很容易描述,但似乎很难解决.问题是加载图标,使用IconDownloader.mApple提供的官方示例提供的自定义类,如果我发布视图,则可以避免崩溃.
我已将IconDownloader该类添加到我的应用程序中,但很明显,只有tableview是root用户才能使用此方法.最大的问题是视图不是根视图.Fe:如果我开始滚动我的第二个视图(应用程序现在加载图标),并且没有时间完成下载,我回到root,应用程序崩溃,因为必须使用新图标更新的视图不再存在了.
一种可能的解决方案可能是OperationQueue在视图中实现一个,但是使用这种方法我在更改视图时停止队列并在我回来时重新启动它并且有N个队列的想法不会让我热情.
有人找到了解决这个问题的好方法吗?
我仍然是Objective C语法的新手,所以我可能会过度复杂,但我似乎无法弄清楚如何将NSTimeInterval传递给线程.
我想启动一个线程,它休眠从主线程发送的x秒参数,如下所示:
[NSThread detachNewThreadSelector:@selector(StartServerSynchThread) toTarget:self withObject:5];
- (void) StartServerSynchThread:(NSTimeInterval *)sleepSecondsInterval {
[NSThread sleepForTimeInterval:sleepSecondsInterval];
}
Run Code Online (Sandbox Code Playgroud)
但是编译器一直给我一个语法错误.我不确定应该怎么做.任何帮助,将不胜感激.谢谢!
假设我有 2 个线程,一个是主线程,另一个是辅助线程。主线程使用得最多,但有时(很少)我希望辅助线程根据主线程的调用做一些工作。大多数时候辅助线程应该休眠。现在经过一些搜索,我明白了这样做的方法是使用 runLoops。所以我试图阅读苹果的文档(http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-SW5 )
但在我看来它非常复杂,我在那里过得很艰难。有没有一种优雅而简单的方法来实现我所描述的?我可以运行和使用任何类似的 runLoop 代码示例吗?
谢谢
当我尝试在线程中执行时:
UITextView *aDescriptionView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 25, 50)];
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
bool _WebTryThreadLock(bool), 0x2819b0: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
Run Code Online (Sandbox Code Playgroud)
这里出了什么问题?我该怎么办?我必须在一个单独的线程中有这个,因为它返回一个UIView对象.
当我做po [NSThread currentThread]时,我得到了
{name =(null),num = 4}
当我向左看时,我看到:

看起来它是线程号6,而不是4.还有什么属性我们需要调用来获取该线程数?
[NSThread currentThread] .number?虽然不存在.
我有一个应用程序,我在后台重复调用方法.我通过以下步骤实现了这一点:
以下是我使用的代码:
- (void) applicationDidFinishLaunching:(NSNotification *)notification
[NSApplication detachDrawingThread:@selector(refreshUserIdPassword) toTarget:self withObject:nil];
}
-(void)refreshUserIdPassword
{
[self getAllUserIdsPasswordsContinousely];
[NSThread sleepForTimeInterval:180];
[self refreshUserIdPassword];
}
Run Code Online (Sandbox Code Playgroud)
我已经读过NSThread不是执行后台任务的最佳方法,并且在cocoa中提供了其他类,例如 - NSOperationQueue和GCD,它应优先于NSThread来执行异步任务.所以我试图使用替代类来实现上面指定的功能.
问题是 - 虽然我能够使用这些类执行异步任务,但我无法使用这些类执行重复性任务(如我的情况).
有人可以对此有所了解并引导我走向正确的方向吗?
如何实现以下块?
我需要在后台运行一些任务.然后在后台任务完成后,一些任务将在主线程中运行.
为什么我使用块是因为我需要更新传递给此方法的视图.
- (void)doALongTask:(UIView *)someView {
[self doSomethingInBackground:^{
// Runs some method in background, while foreground does some animation.
[self doSomeTasksHere];
} completion:^{
// After the task in background is completed, then run more tasks.
[self doSomeOtherTasksHere];
[someView blahblah];
}];
}
Run Code Online (Sandbox Code Playgroud)
或者有更简单的方法来实现这个?谢谢.
我有一个关于autorelease的问题,现在我有以下代码:
int main(int argc, char *argv[]){
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}}
Run Code Online (Sandbox Code Playgroud)
doc在@autoreleasepool {}结尾处说标记为autorelease的对象将收到一条释放消息.但是UIApplicationMain永远不会返回,这意味着流程永远不会到达@autoreleasepool的末尾,那么标记为autorelease的对象将永远不会释放,直到应用程序死掉.自动释放没有意义.....
我问过有人说iOS系统会生成一些线程,(你知道,一个线程,一个runloop).他说runloop会创建自动释放池.所以自动释放对象将在thead或runloop结束时释放.但在大多数情况下我们使用主线程.所以他所说的并不能说服我.
什么是使用自动释放的适当时间.它困惑了我很长一段时间.
我得到另一个观点,当一个runloop结束时,autorelease对象将被释放(主runloop将存在所有应用程序的生命?)所以我不确定..
任何要点和相关文件将不胜感激!
我试图在swift中创建一个线程并发送两个参数.我创建线程和这项工作:
let thread = NSThread(target:self, selector:"getData", object:nil)
thread.start()
Run Code Online (Sandbox Code Playgroud)
但是如何将参数发送到我的func getData?如何使用如下参数创建对象:
let params =...
let thread = NSThread(target:self, selector:"getData", object:params)
thread.start()
...
getData(username: String, password: String) {
...
}
Run Code Online (Sandbox Code Playgroud) nsthread ×10
iphone ×4
cocoa ×3
cocoa-touch ×3
objective-c ×3
ios ×2
autorelease ×1
lazy-loading ×1
nsoperation ×1
nsrunloop ×1
runloop ×1
swift ×1
uitextview ×1
xcode ×1