• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
iOS Multithreading

iOS Multithreading






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.


11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    iOS Multithreading iOS Multithreading Presentation Transcript

    • Threads  Threads are a relatively lightweight way to implement multiple paths of execution inside of an application. Threads let you perform more than one action at the same time, and they execute independently of one another
    •  While the iPhone is not multicore, there are many situations where you would like to use multiple threads to perform some process in the background, while leaving your user interface responsive. Also, at some point iPhone OS devices will go multicore, so designing your application for multiple threads can lead to them to run faster on those future devices.
    • Run loops and Main Thread  When your application first starts, it creates and starts in motion a run loop. This run loop cycles around for the duration of your application, taking in user interface and system events, handling timers, and performing user interface updates, among other functions. All actions of the run loop are performed on the main thread of your application.
    •  What this means is that if you perform a computationally expensive operation, or one that waits for a network service to respond, your applications interface will stop updating and will become unresponsive until that operation is completed. One thing to watch for in multithreading your application is that all user interface updates must be performed on the main thread. 
    • [self performSelectorOnMainThread : @selector (enableButtons)withObject:nil waitUntilDone:YES];will send the message [self enableButtons] on the main thread,and will block the currently executing thread until that methodfinishes on the main thread.
    • To delay your operation until after this update occurs,you can use code like the following:[self performSelector:@selector(delayedAction)withObject:nil afterDelay:0.01];
    • For regular actions that need to be performed at certaintimes, or that can be spread out over a certain duration, youcan use an NSTimer.  For example, secondsTimer = [NSTimerscheduledTimerWithTimeInterval: 1.0f target: selfselector:@selector(respondToTimer) userInfo:nilrepeats:YES];
    • This timer is retained by the current run loop and willterminate and be released when you call [secondsTimer invalidate];
    • Manual NSThreads Creating threads is not as hard as it sounds.  You can run amethod on a background thread using a single command. Either [NSThreaddetachNewThreadSelector:@selector(countingThread)toTarget:self withObject:nil];Or [selfperformSelectorInBackground:@selector(countingThread)withObject:nil];
    • Your overall application has a global autorelease pool in place (look tomain.m to see how this created), but your newly spawned thread doesnot.  If you use an autoreleased object within this thread, you will seewarnings like this appearing all over your console:_NSAutoreleaseNoPool(): Object 0xf20a80 of class NSCFNumberautoreleased with no pool in place - just leakingTo prevent these objects from being leaked, youll need to create a newautorelease pool at the start of your thread: NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];and release it at the end:[pool release];
    • If you have the need to delay the execution of some part of yourthread for a defined time, you can use several functions andmethods.  These includeusleep(10000);which causes your code to delay by 10 milliseconds (10000microseconds) before proceeding, and [NSThread sleepForTimeInterval:0.01];which does the same
    • Introduction  In the web services world, REpresentational State Transfer (REST) is a key design idiom that embraces a stateless client-server architecture in which the web services are viewed as resources and can be identified by their URLs.
    • XML Extensible Markup Language (XML) is a markup language created to structure, store, and transport data by defining a set of rules for encoding documents in a format that is both human-readable and machine-readable. <users> <user> <userName>mspeller</userName> <firstName>Mike</firstName> <lastName>Speller</lastName> </user> <user> <userName>mgdan</userName> <firstName>Mila</firstName> <lastName>Gdan</lastName> </user> ... </users>
    • NSXMLParser  Objective C NSXMLParser is an event driven parser. When an instance of this class pareses an XM document it notifies its delegate about the items (elements, attributes, etc) that it encounters during XML processing. It does not itself do anything with the parsed items – it expects the delegate to implement this code.
    • Three main NSXMLPraser events:NSXMLParser hits the start of an element -parser:didStartElement:namespaceURI:qualifiedName:attributes method notifiedNSXMLParser hits an element value - parser:foundCharactersmethod notifiedNSXMLParser hits the end of an element -parser:didEndElement:namespaceURI:qualifiedNamemethod notified
    • First create a class to store parsed user data:#import <Foundation/Foundation.h>@interface User : NSObject { NSString *userName; NSString *firstName; NSString *lastName;}@property (nonatomic, retain) NSString *userName;@property (nonatomic, retain) NSString *firstName;@property (nonatomic, retain) NSString *lastName;@end
    • #import "User.h"@implementation User@synthesize userName, firstName,lastName;- (void) dealloc { [userName release]; [firstName release]; [lastName release]; [super dealloc];}@end
    • Lets create a delegate for the parser XMLParser.h - it implements thethree events reported by NSXMLParser object:#import <Foundation/Foundation.h>@class User;@interface XMLParser : NSObject { // an ad hoc string to hold element value NSMutableString *currentElementValue; // user object User *user; // array of user objects NSMutableArray *users;}@property (nonatomic, retain) User *user;@property (nonatomic, retain) NSMutableArray *users;- (XMLParser *) initXMLParser;@end
    • NSXMLParser call exampleNSData *data contains our XML document.#import "XMLParser.h"…- (void) doParse:(NSData *)data { // create and init NSXMLParser object NSXMLParser *nsXmlParser = [[NSXMLParser alloc]initWithData:data]; // create and init our delegate XMLParser *parser = [[XMLParser alloc] initXMLParser]; // set delegate [nsXmlParser setDelegate:parser];
    • // parsing... BOOL success = [nsXmlParser parse]; // test the result if (success) { NSLog(@"No errors - user count : %i", [parser [users count]]); // get array of users here // NSMutableArray *users = [parser users]; } else { NSLog(@"Error parsing document!"); } [parser release]; [nsXmlParser release];}
    • Parse the start of an elementImplement method called NSXMLParser when it hits the start of anelement:...// XMLParser.m- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { if ([elementName isEqualToString:@"user"]) { NSLog(@"user element found – create a new instance of User class..."); user = [[User alloc] init]; }}
    • Parse an element valueImplement method called NSXMLParser when it hits an element value. Inthis method we capture the element value into currentElementValue ad hocstring:...// XMLParser.m- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if (!currentElementValue) { // init the ad hoc string with the value currentElementValue = [[NSMutableString alloc] initWithString:string]; } NSLog(@"Processing value for : %@", string);}
    • Parse the end of an elementImplement method called NSXMLParser when it hits the end ofan element:...//XMLParser.m- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if ([elementName isEqualToString:@"users"]) { // We reached the end of the XML document return; }
    • if ([elementName isEqualToString:@"user"]) { // We are done with user entry – add the parsed user // object to our user array [users addObject:user]; // release user object [user release]; user = nil; } else { // The parser hit one of the element values. // This syntax is possible because User object // property names match the XML user element names [user setValue:currentElementValue forKey:elementName]; } [currentElementValue release]; currentElementValue = nil;}// end of XMLParser.m file
    • Dealing with JSON in iPhone 
    •  You can easily use the JSON (JavaScript Object Notation) data format in client-server communications when writing an iPhone app. Unfortunately, Apple iPhone SDK doesn’t come with a built-in JSON parser. But a good one is available called json-framework. It is both a generator and a parser.  As a generator, json-framework can create JSON data from an NSDictionary. As a parser, you can pass to json- framework an NSString that consists of JSON data and it will return a NSDictionary that encapsulates the parsed data.
    • {“menuitem”: [{"value": "New","onclick": "CreateNewDoc()"},{"value": "Open","onclick": "OpenDoc()"},{"value": "Close","onclick": "CloseDoc()"}]}
    • USE SBJSON 