将多个字符串传递给 stringByEvaluatingJavaScriptFromString 时出现问题
我陷入了一个奇怪的问题。我目前正在 iPhone 上开发 Mapkit。我需要在地图中显示两条路线,其中有一个源城市和两个不同的目的地。对于两个城市之间的路线,我的代码很好。为此,在我的代码中的一个地方我正在这样做......
- (void)loadWithStartPoint:(NSString *)startPoint endPoint:(NSString *)endPoint options:(UICGDirectionsOptions *)options {
[googleMapsAPI stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"loadDirections('%@', '%@', %@)", startPoint, endPoint, [options JSONRepresentation]]];
}
在上面的代码中,stringByEvaluatingJavaScriptFromString 由于绘制了路线而将 javascript 传递给我的委托方法。 现在,我必须绘制两条不同的路线,为此我已经更改了上面的代码,如下所示..
- (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];
}
}
}
我有以下内容来创建和实现 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];
}
在这里,我创建了另一个线程,因此我可以单独将字符串传递给 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如阿里所建议的,你可以去wid..
performSelector:withObject:afterDelay:
它会给你想要的结果..你可以像这样编写代码。
希望这会对你有所帮助。
As suggested by Ali u can go wid..
performSelector:withObject:afterDelay:
it will give u desired result..u can write ur code like..
Hope this will help u out.
我猜这是因为多线程不太符合 UIWebView 的要求。
您应该使用 NSOperationQueue 或 GCD 来堆栈 stringByEvaluatingJavaScriptFromString 的调用,以便它们在后台异步执行,但仍然在主线程中执行(使用dispatch_get_main_queue () 或
performSelectorOnMainThread:
等)。如果没有多线程的实际问题,你也可以直接调用
stringByEvaluatingJavaScriptFromString
(为什么要创建线程?即使你想单独传递字符串,你仍然可以多次调用该方法,不是吗? ?)您还可以尝试使用
performSelector:withObject:afterDelay:
(延迟为 0 或 0.01),以便在运行循环的下一次迭代期间执行调用。一般来说,如果您确实不需要使用它们,请避免使用线程(有关 Apple 文档中的详细信息,请参阅“并发编程指南”和“线程编程指南”)。当异步方法存在时,优先使用它们,然后使用 NSOperationQueues 或 GCD(只有当你没有任何其他解决方案时,才可以使用 NSThreads)。这是因为更高的 API 将为您管理棘手的事情,并使处理多线程时的问题变得不那么复杂。
I guess this is due to multithreading not being very compliant with the UIWebView.
You should use
NSOperationQueue
or GCD to stack your calls ofstringByEvaluatingJavaScriptFromString
so that they are executed asychronously in the background, but still execute them in the main thread (usedispatch_get_main_queue()
orperformSelectorOnMainThread:
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.