-5

is it possible to call an action that contain heavy operations several times? like save button (I save the first object) and want to add others, but the app becomes so slow and I can't navigate to other views

Ouassim Mouyarden
  • 307
  • 1
  • 4
  • 7
  • Please be more specific with your question. Show us some code. – Luke Aug 06 '13 at 17:29
  • 1
    on concurrency: http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008091 – Greg Price Aug 06 '13 at 17:30
  • Before any suggestions on fixes can be given, you must find out *why* it is slow. See http://stackoverflow.com/questions/9263783/iphone-time-profile-instrument for information on how to profile your application to narrow down what's going on. – BergQuester Aug 06 '13 at 17:44

2 Answers2

1

You need to make sure that you aren't tying up the main UI thread of your application. A potential easy fix is to do your save operation on a different thread. Here is an example of how to do just that using Grand Central Dispatch: iPhone - Grand Central Dispatch main thread

Example:

//notice this saveQueue is a new dispatch queue that's been created.
dispatch_queue_t saveQueue = dispatch_queue_create("com.mycompany.myqueue", 0);

dispatch_async(saveQueue, ^{
    //this command represents your long running operation
    doSaveOperation();

    dispatch_async(dispatch_get_main_queue(), ^{
        //always update your UI on the main thread!
        [self showCompleteMessage];
    });    
});
Community
  • 1
  • 1
Ralph Caraveo
  • 10,025
  • 7
  • 40
  • 52
  • thank you for reply : what's showCompleteMessage ? – Ouassim Mouyarden Aug 06 '13 at 17:50
  • @OuassimMouyarden that is just an example of how you would call a method to update your UI if you need to prompt the user when the save is completed. It's not necessary but good practice to tell the user their save operation is complete. – Ralph Caraveo Aug 06 '13 at 17:51
  • now when will try to call doSaveOperation again, even if it's not completed yet for my first call, is the same thread will be used? – Ouassim Mouyarden Aug 06 '13 at 18:03
1

You can always play around with heavy data saving related tasks in the background. You can distribute the content related stuff to various threads (queues), you need to separate the stuff which you are saving and if it is blocking your UI and making your app slow then you need to perform these heavy operations in the background.

Make sure, you do NOT perform any UI updation operation in the background. Try reading about GCD (how it works), how you can create a background Queue etc and how you can play around with it. I assume you might be using core data in order to save the contents on save button. Try reading about the Parent/Child Manage Context Objects. Play around with it a bit and move your heavy task to background, updation of UI will always be on Main or UI thread (which you can always do it by calling get_main_queue() if you are working with dispatch queues). Happy Coding. If you have specific code, which is doing this, then let us know. We will be glad to help. :)

Reno Jones
  • 1,979
  • 1
  • 18
  • 30
  • this what I try to do but makesz the app so slow : dispatch_async(dispatch_get_main_queue(), [self saveMethod:]; }); where saveMethod is the method that contain heavy operations – Ouassim Mouyarden Aug 06 '13 at 17:38
  • @OuassimMouyarden you need to make sure you use a different queue other than the main queue. Otherwise, it's as if you are attempting to do this operation on the main thread. In the link to my answer, a new dispatch queue is created that is NOT the main queue. – Ralph Caraveo Aug 06 '13 at 17:40
  • You are doing this on the main, you need to create your own queue and try playing around with it. This is the main queue - dispatch_get_main_queue(). you need to shift the momentum to another thread. – Reno Jones Aug 06 '13 at 17:40
  • I am new in objective-C developpemnt how can I do it with othee thread please? – Ouassim Mouyarden Aug 06 '13 at 17:44
  • @OuassimMouyarden I updated the answer with code similar to the link I provided. – Ralph Caraveo Aug 06 '13 at 17:45
  • now when will try to call doSaveOperation again, even if it's not completed yet for my first call, is the same thread will be used? and my app will not be slow – Ouassim Mouyarden Aug 06 '13 at 18:41