iCoder4777
iCoder4777

Reputation: 1692

Problem in passing multiple strings to stringByEvaluatingJavaScriptFromString

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.

Upvotes: 0

Views: 1102

Answers (2)

user859045
user859045

Reputation:

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.

Upvotes: 2

AliSoftware
AliSoftware

Reputation: 32681

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.

Upvotes: 0

Related Questions