我有一个带有表视图的导航视图,当单击一行时,行indexPath将传递给下一个视图.
在Details视图viewDidLoad中,我从Core Data获取数据.我使用应用程序委托中的提取
[appDelegate loadItem:i];
正如您所看到的,我只传递一个带有行号的整数.
问题是:如何在另一个线程中进行此调用过程.(在后台)我需要这个,因为有时获取的结果太大,因此处理需要3秒延迟推送详细信息视图.
我需要显示三秒的活动指示器,所以我需要将Fetch放在另一个线程中,以便在处理提取时能够使用指示器的UI.
我想要最简单的方式,因为我是新手.发布一些代码会很棒.或链接:)
我在NSOperationQueue中运行了NSOperation.NSOperation下载一些数据并将其解析为NSDictionary.我如何知道NSOperation何时完成并获得该字典?谢谢.
我正在尝试在NSOperationQueue设置的线程上运行NSTimer
-(void)apiCallbackQueueManager:(NSString *)callid :(NSString *)service:(NSString *)action:(NSString *)data{
SEL theSelector = @selector(apiCallbackQueueTimer: service: action: data:);
NSMethodSignature * sig = [self methodSignatureForSelector:theSelector];
NSInvocation * theInvocation = [NSInvocation invocationWithMethodSignature:sig];
[theInvocation setTarget: self];
[theInvocation setSelector: theSelector];
[theInvocation setArgument: &callid atIndex: 2];
[theInvocation setArgument: &service atIndex: 3];
[theInvocation setArgument: &action atIndex: 4];
[theInvocation setArgument: &data atIndex: 5];
NSInvocationOperation* operation = [[NSInvocationOperation alloc] initWithInvocation:theInvocation];
NSOperationQueue *apiQueue = [[NSOperationQueue alloc] init];
[apiQueue addOperation:operation];
}
-(void)apiCallbackQueueTimer:(NSString *)arg1 service:(NSString *)arg2 action:(NSString *)arg3 data:(NSString *)arg4{
apiTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self …Run Code Online (Sandbox Code Playgroud) 我正在使用NSOperation(s)下载文件并添加它们NSOperationQueue.NSOperationQueue当应用程序进入后台时,它会被暂停.如果队列不在后台暂停并开始下一个操作,是否有任何解决方法?
multithreading nsoperation nsoperationqueue grand-central-dispatch ios
我的网络活动指示器有问题,有时它会在不应该显示时继续显示.
我为它编写了我自己的经理并将其换成了一个使用这样的NSAssert语句的人...
- (void)setNetworkActivityIndicatorVisible:(BOOL)setVisible {
static NSInteger NumberOfCallsToSetVisible = 0;
if (setVisible)
NumberOfCallsToSetVisible++;
else
NumberOfCallsToSetVisible--;
// The assertion helps to find programmer errors in activity indicator management.
// Since a negative NumberOfCallsToSetVisible is not a fatal error,
// it should probably be removed from production code.
NSAssert(NumberOfCallsToSetVisible >= 0, @"Network Activity Indicator was asked to hide more often than shown");
// Display the indicator as long as our static counter is > 0.
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:(NumberOfCallsToSetVisible > 0)]; …Run Code Online (Sandbox Code Playgroud) network-programming objective-c nsoperation nsoperationqueue ios
我写了以下类别 NSOperationBlock
@implementation NSOperationQueue (Extensions)
-(void)addAsynchronousOperationWithBlock:(void (^)(block))operationBlock
{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
block signal = ^ {
dispatch_semaphore_signal(semaphore);
};
[self addOperationWithBlock:^{
operationBlock(signal);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_release(semaphore);
}];
}
@end
Run Code Online (Sandbox Code Playgroud)
它似乎工作正常,但当我调用它时(如下面的代码段所示)我得到一个警告:
阻止可能会导致保留周期
[_queue addAsynchronousOperationWithBlock:^(block signal) {
[self foo:nil];
signal();
}];
Run Code Online (Sandbox Code Playgroud)
foo 是使用此类别的类的方法.
与addOperationWithBlock:(from NSOperationQueue)相同的代码不显示警告:
[_queue addOperationWithBlock:^ {
[self foo:nil];
}];
Run Code Online (Sandbox Code Playgroud)
我真的不明白.特别是我不明白的是:我是否应该在两种情况下都使用弱指针?实际上,如果我不使用弱指针,两个片段会带来保留周期吗?
这是我的应用场景:当用户滑动通知时,我将通过URL启动一些其他应用.因此,当通知到达时,它基本上会启动其他应用程序.
目前处理滑动通知方案的时候
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)
Run Code Online (Sandbox Code Playgroud)
方法被调用,在这个方法中,我调用processNotification:方法,其中包含:
...
[[UIApplication sharedApplication] openURL:url];
...
Run Code Online (Sandbox Code Playgroud)
url则打开完全正常.url则在后台打开,但当前查看的应用程序是我的应用程序.例如,如果我url是tel:123-456-7890,iOS启动呼叫(你可以听到声音)但活动的应用程序不是Phone.app,它是我的应用程序.这对我来说似乎很奇怪.但是,如果我等待UI加载,然后调用processNotification:它,它会Phone.app正确显示窗口.(平台中的错误?因为调用发生但我的UI位于顶部.)
我需要一种方法来延迟执行此processNotification:调用(可能通过操作队列),直到加载视图控制器.否则,我的应用程序将保持在最顶层,并在后台打开URL.
apple-push-notifications nsoperationqueue grand-central-dispatch ios
说我想实现这样的模式:
a = some array I download from the internet
b = manipulate a somehow (long operation)
c = first object of b
Run Code Online (Sandbox Code Playgroud)
这些显然需要同步调用,这导致我在目标C中出现问题.我读过NSOperationQueueGCD,我不太了解它们,或者哪些适合这里.有人可以建议一个解决方案?我知道我也可以使用performSelector:@selector(sel)WaitUntilDone,但对于大型操作而言,这似乎并不高效.
cocoa-touch objective-c nsoperationqueue grand-central-dispatch ios
这是我的代码:
@interface MyObject ()
@property(nonatomic) dispatch_queue_t queue;
@end
@implementation MyObject {
NSThread *_check;
}
- (id)init {
self = [super init];
if (self) {
_queue = dispatch_queue_create("com.Thread.queue", NULL);
dispatch_async(_queue, ^{
_check = [NSThread currentThread]; //for ex. thread number = 3
//some code here...
});
}
return self;
}
- (void)someMethod:(MyObjClass *)obj {
dispatch_async(_queue, ^{
//need th
if (_check != [NSThread currentThread]) { // it is sometimes number 3, but sometimes it changes
NSLog(@"Thread changed.");
}
[obj doSmth]; //got crash if …Run Code Online (Sandbox Code Playgroud) 我尝试NSBlockOperation在完成或取消操作后重新启动,但是出现错误?任何人都知道哪里出错了吗?谢谢
let imageURLs = ["http://www.planetware.com/photos-large/F/france-paris-eiffel-tower.jpg",
"http://adriatic-lines.com/wp-content/uploads/2015/04/canal-of-Venice.jpg",
"http://algoos.com/wp-content/uploads/2015/08/ireland-02.jpg",
"http://bdo.se/wp-content/uploads/2014/01/Stockholm1.jpg"]
class Downloader {
class func downloadImageWithURL(url:String) -> UIImage! {
let data = NSData(contentsOfURL: NSURL(string: url)!)
return UIImage(data: data!)
}
}
class ViewController: UIViewController {
@IBOutlet weak var imageView1: UIImageView!
var indeX = 0
let operation1 = NSBlockOperation()
var queue = NSOperationQueue()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func didClickOnStart(sender: AnyObject) {
queue = NSOperationQueue()
operation1.addExecutionBlock { () -> Void in
for _ in imageURLs {
if !self.operation1.cancelled {
let …Run Code Online (Sandbox Code Playgroud) nsoperationqueue ×10
ios ×9
nsoperation ×4
objective-c ×4
cocoa-touch ×2
core-data ×1
iphone ×1
nsthread ×1
nstimer ×1
swift ×1
swift2 ×1