:0:too much recursion">

Cocos2D-JS中报JS: :0:too much recursion

昨晚在Xcode中调用JS的代码,发现一直报错。

JS: :0:too much recursion

原因是我在HTTP请求的回掉中,调用了。JS的函数

.....................................
NSURLSessionDataTask* task = [session dataTaskWithRequest:reqM completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (!error) {
            NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            .....................................
                .....................................
                    .....................................

                    //调用了下面的代码,代码的正确✅的。但是在错的地方调用的。实际观察这里是子线程。
                        std::string jsString = [@"hhh" cStringUsingEncoding:NSUTF8StringEncoding];
                        std::string jsCallStr = cocos2d::StringUtils::format("provideContent(\"%s\")",jsString.c_str());
                        ScriptingCore::getInstance()->evalString(jsCallStr.c_str());

            NSLog(@"%@",dic);
        }else {
            NSLog(@"%@",error);
        }
    }];
    
    [task resume];

正确是的方式应该是

.....................................
NSURLSessionDataTask* task = [session dataTaskWithRequest:reqM completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (!error) {
            NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            .....................................
                .....................................
                    .....................................

                    //在现场调用才是正确✅的。
                        dispatch_async(dispatch_queue_create(0, 0), ^{
                    // 子线程执行任务(比如获取较大数据)
                    dispatch_async(dispatch_get_main_queue(), ^{
                        // 通知主线程刷新 神马的
                        std::string jsString = [@"hhh" cStringUsingEncoding:NSUTF8StringEncoding];
                        std::string jsCallStr = cocos2d::StringUtils::format("provideContent(\"%s\")",jsString.c_str());
                        ScriptingCore::getInstance()->evalString(jsCallStr.c_str());
                    });
                });

            NSLog(@"%@",dic);
        }else {
            NSLog(@"%@",error);
        }
    }];
    
    [task resume];

原帖:http://www.jianshu.com/p/6beb03b71cdd

你可能感兴趣的:(Cocos2D-JS中报JS: :0:too much recursion)