0

I'm stuck in a weird problem. I am currently working on mapkit on iPhone. I need to show two routes in my map, for which there is a source city and two different destination. For a route between two cities my code was fine. for that purpose in one place in my code i was doing like this….

- (void)loadWithStartPoint:(NSString *)startPoint endPoint:(NSString *)endPoint options:(UICGDirectionsOptions *)options {
[googleMapsAPI stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"loadDirections('%@', '%@', %@)", startPoint, endPoint, [options JSONRepresentation]]];
}

In the above code stringByEvaluatingJavaScriptFromString was passing javascript to my delegate method due to which route was drawn. Now, i have to draw two different routes, for that purpose i have changed above code like this..

- (void)loadWithStartPoint:(NSString *)startPoint endPoint:(NSMutableArray *)endPoints options:(UICGDirectionsOptions *)options {
    for (int idx = 0; idx < [endPoints count];idx ++) 
    {
        NSString* msg = [NSString stringWithFormat:@"loadDirections('%@', '%@', %@)", startPoint, [endPoints objectAtIndex:idx], [options JSONRepresentation]];
        mstr = [msg retain];
        if (idx == 0)
        {
            [googleMapsAPI stringByEvaluatingJavaScriptFromString:msg];
        }
        else {
            [NSThread detachNewThreadSelector:@selector(loadroute:) toTarget:self withObject:mstr];
        }
    }
}

i have the following to create and implement NSThread.

-(void)loadroute :(NSString *)message
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    [self performSelectorOnMainThread:@selector(loadComplete:) withObject:message waitUntilDone:YES];
    [pool release];

}

-(void)loadComplete:(NSString *)message
{
    [googleMapsAPI stringByEvaluatingJavaScriptFromString:message];
}

here, i have created another thread due to which i would be able to pass strings to stringByEvaluatingJavaScriptFromString separately. But only the last string is passed to the delegate method.What i'm missing? please help me out. i'm stuck in this weird problem since last week. Any help would be appreciated. Thnx in advance.

2 Answers 2

2

As suggested by Ali u can go wid..performSelector:withObject:afterDelay:it will give u desired result.. u can write ur code like..

 - (void)loadWithStartPoint:(NSString *)startPoint endPoint:(NSMutableArray *)endPoints options:(UICGDirectionsOptions *)options {
            for (int idx = 0; idx < [endPoints count];idx ++) 
            {
    NSString* msg = [NSString stringWithFormat:@"loadDirections('%@', '%@', %@)", startPoint, [endPoints objectAtIndex:idx], [options JSONRepresentation]];
            mstr = [msg retain];

             [self performSelector:@selector(loadComplete:) withObject:nil afterDelay:0.5];
            }
        }

-(void)loadComplete:(NSString *)message
 {
        [googleMapsAPI stringByEvaluatingJavaScriptFromString:message];
 }

Hope this will help u out.

Sign up to request clarification or add additional context in comments.

Comments

0

I guess this is due to multithreading not being very compliant with the UIWebView.

You should use NSOperationQueue or GCD to stack your calls of stringByEvaluatingJavaScriptFromString so that they are executed asychronously in the background, but still execute them in the main thread (use dispatch_get_main_queue() or performSelectorOnMainThread: etc).

If there is no real matter about multithreading, you may also simply call stringByEvaluatingJavaScriptFromString directly (why creating a thread? You can still call the method multiple times even if you want to pass strings separately, don't you?)

You may also try using performSelector:withObject:afterDelay: (with a delay of 0 or 0.01) so that the call will be executed during the next iteration of the runloop.

In general, if you don't really need to use them, avoid using threads (see "Concurrency Programming Guide" and the "Threading Programming Guide" for details info in Apple's doc). Prefer using asynchronous methods when they exists, then NSOperationQueues or GCD (and only if you don't have any other solution, you may use NSThreads). This is because higher APIs will manage tricky stuff for you and make problems less complex when dealing with multithreading.

1 Comment

Thnx Ali for showing me the clear picture of NSThread & NSOperationQueue. I will try to use any one of ur suggested way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.