Thursday, April 12, 2012

Implement multi threaded / asynchronous delegate calls

When implementing own classes with a delegate protocol it is very simple to achieve an asynchronous behaviour. To explain how it works ill describe a simple scenario:

You have a class that searches big amount of text for keywords. This class should have a delegate protocol that calls following methods:

//this function is called whenever the class finds a string in the text and delivers an array containing page number, line and text excerpt
-(void)didFindTextOnPage:(NSArray*)arrData;
//function is called when search has finished
-(void)didFinishSearching;

When these methods are called in a different thread, on the main thread e.g. a UITableView could be continuously populated with the search results and a loader could be shown to notify the user that the search is still active. When the search is finished the loader can be removed.

All this is easily achieved with following snippet:

------------------------------------------------------------------


//start different thread
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   
   //do something in a different thread
   dispatch_async( dispatch_get_main_queue(), ^{
       //thread has finished so call final method
   });
});

------------------------------------------------------------------

In the first line we start a new thread. Everything within this "loop" wont affect the main thread.
dispatch_async... is the callback that is called when the "loop" has finished his operations.

In our search example the above code could be used as follows to have a multithreaded approach:

------------------------------------------------------------------

//start different thread
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                
    //loop over pages
   for(int i = 0; i < [pages count]; i++){

      NSArray* arrLines = [NSDicionary objectAtIndex:i];

      //loop over lines
      for(int j = 0; j < [arrLines count]; j++){

         NSArray* arrLine = [arrLines objectAtIndex:j];

         //if keyword exists
         if([self keyWordExistsOnPage:i forLine:j] == YES){

            //call the delegate method
            [self.delegate didFindTextOnPage: arrLine];
         }
      }
    }

   //callback for thread
   dispatch_async( dispatch_get_main_queue(), ^{
      //thread has finished so call final method
      [self.delegate didFinishSearching];
   });
});


------------------------------------------------------------------

Hope this helps. Let me know in the comments if something could be improved here or if you have another approach.


Happy coding :)

Sunday, April 8, 2012

Saturday, April 7, 2012

iOS TTS (text-to-speech) offline and multilingual


I am currently working on a project where I should add text to speech functionlity.
I searched different blogs and forums and found several services that do TTS conversion via a webservice.

The problem is I need an offline library that can "speak" several languages. The only one I found was: http://espeak.sourceforge.net/
this lib is okay in the simulator but for some reason on the device (ipad 2 & 3) the speech is horrible and speaks out single umlauts and has very strange pronunciation...

Does somebody know a good solution for this problem (other lib or how to make eSpeak work correctly on the iPad)?

Let me know in the comments?

Thanks and happy coding :)

scrollable label / text


The last years i used a uiscrollview and added a label onto it. After that i calculated the size of the text height and did set the contentSize property of the scrollview. All this is not necessary by using a simple trick.

I wasn't aware of this before and maybe most people do it this way but in case somebody didn't know it here the very simple solution:

add a textview and set the editable property to no. That's it.

Happy coding :)