0

I'm trying to fixe an issue that I can see on my iDevice but not on my Mac (obviously).

I've marked a very high CPU activity on this line:

data = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)];

This appears when I'm downloading and showing a HD picture (4-5Mo).

Is anything wrong in my code or am I doing something really bad ?

Edit

Finally this line was not that important. My problem was that I thought that my asynchronous method was in another thread. It wasn't.

I start thinking about new thread with:

[NSThread detachNewThreadSelector:@selector(test:) toTarget:self withObject:myObject];

For my problem, is it the best way to do it ?

halfer
  • 19,824
  • 17
  • 99
  • 186
brcebn
  • 1,571
  • 1
  • 23
  • 46

2 Answers2

0

Basically whenever possible, CPU intensive operations are supposed to be performed on the background thread to avoid blocking of any user interaction in your application. Hope you are taking care of that.

Also your's being a process associated with image construction/manipulation will tend to be CPU intensive.

Vijay Tholpadi
  • 2,135
  • 1
  • 15
  • 20
  • Everything is in background obviously but I'm afraid about the time that this operation is taking. I don't have any idea of the average duration of this operation. Do you have some reference about that ? Maybe a mathematical function exists to calculate the duration of the treatment according to the size/resolution of a picture ? If i can forecast the duration I can resize the picture if necessary. – brcebn Jun 15 '14 at 14:58
  • You are over-thinking this. JPEG compression is CPU-intensive. If you use GCD to do the work in the background, it won't block the UI. Just let it run. – Duncan C Jun 15 '14 at 17:36
  • I found a part of my problem. I thought that asynchrone download was on another thread but finally not. I tried to do it with `NSThread` `detachNewThreadSelector:toTarget:withObject:` but I can't stop it. That means I have a new leak each time I call my method. – brcebn Jun 16 '14 at 18:52
  • You should really have a look at Grand Central Dispatch. – gnasher729 Jun 16 '14 at 23:58
0

There is a solution here very well explain for the problem of non-blocking thread using NSManagedObjectContext with children.

For the other problem( high CPU activity) is was due to this non-blocking thread problem.

Community
  • 1
  • 1
brcebn
  • 1,571
  • 1
  • 23
  • 46