我想在线程完成后安排一个线程.
可能吗 ?怎么样?
例如(指明我的需要)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// 1. response - schedule myThread
// 2. response - schedule a new thread which will be executed after myThread
// 3. response - schedule a new thread which will be executed after second thread
// .....
}
Run Code Online (Sandbox Code Playgroud) 我使用以下代码将对象传递给辅助线程:
(void)login:(id)sender
{
platformMsgs_LoginRequest *loginRequest = [[[platformMsgs_LoginRequest alloc] init] autorelease];
//more code...
[NSThread detachNewThreadSelector:@selector(sendLoginRequest:) toTarget:self withObject:loginRequest];
//more code...
}
- (void)sendLoginRequest:(platformMsgs_LoginRequest *)loginRequest
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[loginRequest retain];
NetSuiteBinding *binding = [NetSuiteServiceSvc NetSuiteBinding];
NetSuiteBindingResponse *response = [binding loginUsingParameters:loginRequest applicationInfo:nil partnerInfo:nil];
[self performSelectorOnMainThread:@selector(loginOperationCompleted:) withObject:response waitUntilDone:NO];
[loginRequest release];
[pool drain];
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,自动释放正确的方法来处理这个对象的释放吗?一旦它传递给辅助线程,我保留它并在我不再需要它时释放它.
但是,自动释放可能会在辅助线程有机会保留它之前释放对象吗?我是否必须为此创建一个ivar?以便我可以在performSelectorOnMainThread中释放该对象?登录后我不再需要该对象,因此ivar似乎不是正确的方法.处理这个问题的最佳方法是什么?谢谢.
-Oscar
我在NSThread中使用NSURLConnection,但没有执行NSURLConnection的委托方法!我的NSTread子类中有一个main方法,一个while循环使线程保持活动状态.有帮助吗?
很抱歉所有这些代码,但我认为这是描述我的问题的最佳方式.所以这是一个执行异步连接调用的对象createConnectionWithPath:userObjectReference
@interface WSDAsyncURLConnection : NSObject
{
NSMutableData *receivedData;
NSDate *connectionTime;
NSURLConnection *connection;
id _theUserObject;
}
@property (nonatomic, retain) NSMutableData *receivedData;
@property (nonatomic, retain) NSDate *connectionTime;
@property (nonatomic, assign) NSURLConnection *connection;
- (void)createConnectionWithPath:(NSString *)thePath userObjectReference:(id)userObject;
@end
#import "WSDAsyncURLConnection.h"
@implementation WSDAsyncURLConnection
@synthesize connectionTime, receivedData, connection;
- (void) terminate
{
if (self.connection) {
[self.connection release];
self.connection = nil;
}
}
- (void) createConnectionWithPath:(NSString *)thePath userObjectReference:(id)userObject;
{
_theUserObject = userObject;
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:thePath]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
self.connection = [[NSURLConnection alloc] …Run Code Online (Sandbox Code Playgroud) 我有一个菜单,其中的项目在3秒的间隔内一个接一个地弹出,我这样做:
for(UIButton *menuItem in menuItems){
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (0.3 * i) * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
[menuItem setAlpha:1.0];
}
}
Run Code Online (Sandbox Code Playgroud)
是否可以在中间停止动画(例如,当触摸按钮时)?我尝试将所有内容设置为alpha 1.0但正如预期的那样,线程继续运行并再次显示项目.
会欣赏任何想法:)
Shai.
我想创建一个不是主线程的工作线程,所以我可以调用...
[self performSelector:@selector(doStuff) OnThread:self.myWorkerThread withObject:nil];
Run Code Online (Sandbox Code Playgroud)
...在我的代码中的一堆位置.如何创建线程..任何线程并将其分配给变量,以便我可以在我的代码中反复使用它.NSThread detachNewThreadWithSElector只会创建一个用于运行该选择器的目的.我需要创建一个线程并以某种方式使用它.
我正在使用iOS,需要在writerThread上执行所有CoreData写操作.我每次都需要使用相同的线程(不是主线程).
我已经在xcode中创建了一个线程,我已经给出了从该线程调用的函数名称.但我的问题是没有调用给调用的函数名称(在该函数中放置断点时才知道)
码:
NSThread* myThread;
[myThread start];
[self performSelector:@selector(func1:) onThread:myThread withObject:nil waitUntilDone:false]
Run Code Online (Sandbox Code Playgroud)
后来我也尝试了这个:
NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(func1:)object:nil];
[myThread start];
Run Code Online (Sandbox Code Playgroud)
上面的func1是要调用的函数的名称.
那么任何人都可以告诉我如何创建线程并从那里调用func1 ....
NSOperationQueue创建了许多线程,正如您所期望的那样.但是当您暂停应用程序并在Xcode中调试它时,不清楚哪些线程属于一个操作队列,哪些线程属于另一个操作队列.
我试过了:
[NSThread currentThread] setName: @"My amazing operation thread"]
Run Code Online (Sandbox Code Playgroud)
但是当线程被重用时,这只意味着许多线程获得此名称,然后永远不会丢失它.我试过设置在线程名称-start和它解封-finish,但该线程的名字在Xcode调试线程列表从未露面.
命名线程/操作以使它们在Xcode中更容易调试的好方法是什么?
谁能解释一下 [NSThread currentThread] 返回值的含义?
日志
NSLog(@"%@", [NSThread currentThread]);
Run Code Online (Sandbox Code Playgroud)
结果
<NSThread: 0x1e04ed60>{name = (null), num = 5}
Run Code Online (Sandbox Code Playgroud)
以下是什么?
- “NSThread:0x1e04ed60”
- 名称 = (空)
- 数量 = 5
num = 5 与 Xcode 中列出的线程编号没有任何关系(它在此实例中显示了线程 9,因为我正在使用 NSOperationQueue 运行多个线程)
苹果文档的解释非常无用,返回值代表当前执行线程的线程对象。 https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html
谢谢!
在当前的iOS应用程序中,我使用此执行选择器方法:
[self performSelector:@selector(doSomething)
onThread:myThread
withObject:nil
waitUntilDone:NO
modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
Run Code Online (Sandbox Code Playgroud)
我不知道如何在swift中的特定线程上运行选择器.有什么建议?
我有一个这样的功能:
func foobar() {
let targetThread = Thread.current
DispatchQueue.main.async {
UIView.animate(withDuration: 0.5, animations: {
// Code I need to run on the main thread
}, completion: {
// Code I want to run on targetThread -- HOW?
}
}
}
Run Code Online (Sandbox Code Playgroud)
我如何从 执行代码targetThread?谢谢。
nsthread ×10
ios ×6
objective-c ×4
iphone ×2
swift ×2
arguments ×1
cocoa ×1
cocoa-touch ×1
connection ×1
debugging ×1
delegation ×1
nsoperation ×1
retain ×1
url ×1
variables ×1
xcode ×1