简体   繁体   English

将多个字符串传递给stringByEvaluatingJavaScriptFromString时出现问题

[英]Problem in passing multiple strings to stringByEvaluatingJavaScriptFromString

I'm stuck in a weird problem. 我陷入了一个奇怪的问题。 I am currently working on mapkit on iPhone. 我目前正在使用iPhone上的mapkit。 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. 在上面的代码中,stringByEvaluatingJavaScriptFromString将javascript传递给我的委托方法,因为它绘制了哪条路由。 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. 我有以下内容来创建和实现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. 在这里,我创建了另一个线程,因为我可以将字符串分别传递给stringByEvaluatingJavaScriptFromString。 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. Thnx提前。

As suggested by Ali u can go wid.. performSelector:withObject:afterDelay: it will give u desired result.. u can write ur code like.. 正如Ali所说,你可以去wid .. performSelector:withObject:afterDelay:它会给你想要的结果..你可以写你的代码如...

 - (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. 希望这会帮助你。

I guess this is due to multithreading not being very compliant with the UIWebView. 我想这是因为多线程不兼容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). 您应该使用NSOperationQueue或GCD来堆叠对stringByEvaluatingJavaScriptFromString的调用,以便它们在后台异步执行,但仍然在主线程中执行它们(使用dispatch_get_main_queue()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?) 如果没有关于多线程的真正问题,你也可以直接调用stringByEvaluatingJavaScriptFromString (为什么要创建一个线程?你仍然可以多次调用该方法,即使你想单独传递字符串,不是吗?)

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. 您也可以尝试使用performSelector:withObject:afterDelay:延迟为0或0.01),以便在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). 通常,如果您不需要使用它们,请避免使用线程(有关Apple文档中的详细信息,请参阅“并发编程指南”和“线程编程指南”)。 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). 首选使用异步方法,然后使用NSOperationQueues或GCD(只有在没有任何其他解决方案的情况下,才可以使用NSThreads)。 This is because higher APIs will manage tricky stuff for you and make problems less complex when dealing with multithreading. 这是因为更高的API将为您管理棘手的东西,并在处理多线程时使问题变得不那么复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM