NSOperation inside NSOperationQueue fails

I really need help here. I'm desperate.

I have an NSOperation that when added to NSOperationQueue does not start. I added some entries to see the status of NSOperation, and this is the result:

Queue operations count = 1 Queue isSuspended = 0 Operation isCancelled? = 0 Operation isConcurrent? = 0 Operation isFinished? = 0 Operation isExecuted? = 0 Operation isReady? = 1 Operation dependencies? = 0 

The code is very simple. Nothing special.

  LoadingConflictEvents_iPad *loadingEvents = [[LoadingConflictEvents_iPad alloc] initWithNibName:@"LoadingConflictEvents_iPad" bundle:[NSBundle mainBundle]]; loadingEvents.modalPresentationStyle = UIModalPresentationFormSheet; loadingEvents.conflictOpDelegate = self; [self presentModalViewController:loadingEvents animated:NO]; [loadingEvents release]; ConflictEventOperation *operation = [[ConflictEventOperation alloc] initWithParameters:wiLr.formNumber pWI_ID:wiLr.wi_id]; [queue addOperation:operation]; NSLog(@"Queue operations count = %d",[queue operationCount]); NSLog(@"Queue isSuspended = %d",[queue isSuspended]); NSLog(@"Operation isCancelled? = %d",[operation isCancelled]); NSLog(@"Operation isConcurrent? = %d",[operation isConcurrent]); NSLog(@"Operation isFinished? = %d",[operation isFinished]); NSLog(@"Operation isExecuted? = %d",[operation isExecuting]); NSLog(@"Operation isReady? = %d",[operation isReady]); NSLog(@"Operation dependencies? = %d",[[operation dependencies] count]); [operation release]; 

Now my operation does a lot of things according to the main method, but the problem is never caused. The main thing is never fulfilled. The strangest thing (believe me, I'm not crazy yet ..). If I put a breakpoint on any NSLog line or when creating an operation, the main method will be called and everything will work fine.

It works well for a long time. I recently made some changes, and apparently something is to blame. One of these changes was updating the device to iOS 5.1 SDK (iPad).

To add something, I have an iPhone version (iOS 5.1) of this application that uses the same NSOperation object. The difference is only in the user interface, and everything works fine.

Oh, and it just doesn't work on the device itself. Everything works fine in the simulator.

Any help would be really appreciated.

Hi,

+4
source share
4 answers

Well, I finally solved this problem.

The problem I ran into is that NSOperation is running in the background all the time (but in a different queue). This operation blocked any other thread (NSOperation) that should be executed. This only happened on the iPad 1 because it is not a dual core.

My NSOperation did something like this on the main method:

 - (void)main { while (![self isCancelled]) { //Do stuff } } 

And NSOperation did it all the time

Stupid for me, I did not give the OS time to work with other threads, so adding sleep did the trick

 - (void)main { while (![self isCancelled]) { [NSThread sleepForTimeInterval:0.5]; //Do Stuff } } 

This dream gives the OS the ability to work with other threads.

Why put a breakpoint at work? ... the debugger stopped all threads and because the breakpoint was in my other NSOperation, this thread was executed.

+3
source

If the operation is parallel, you need to implement -start, not -main.

+5
source

I had the same problem, but my resolution was not the same, so I thought that in the future I would be responsible for future people like me.

My problem was that in my NSOperation subclass, I had:

 @property CGPoint start; 

which during synthesis creates a method:

 -(CGPoint)start 

which overrides NSOperation - (void) start; a method that is meaningful for incompatible NSOperation, and prevents all the usual things that went on (void) from starting to happen, which thus prevents the main method (void) from being called at all.

As soon as I renamed my property to something other than start, it worked fine.

+2
source

Well, this problem only occurs with iPad 1 devices with an application compiled with SDK 5.1. I tried with iPad 1 with iOS 4.2.1 and iPad 1 with iOS 5.1. Both give the same problems.

Of course, this worked on both iPads with an app compiled with SDK 4.3

0
source

Source: https://habr.com/ru/post/1403514/


All Articles