【问题标题】:Js class extending return too fastJs类扩展返回太快
【发布时间】:2018-12-12 18:03:41
【问题描述】:

我正在使用 javaScript 在 nativeScript 中构建任务调度程序。

JobScheduler 调用需要执行的 JobService。

但是 JobService 的返回速度太快了。

您可以看出我使用了异步函数nearby(),它工作正常,但我无法用nearby() 大括号包裹整个脚本。

所以我需要一些方法来延迟返回语句,直到我拥有所有数据。

scheduler.js:

function scheduleJob(context) {

        // Create a component from the JobService that should be triggered
        var component = new android.content.ComponentName(context, com.tns.notifications.MyJobService.class);

        // Set the id of the job to something meaningful for you
        const builder = new android.app.job.JobInfo.Builder(1, component);

        // Optional: Set how often the task should be triggered. The minimum is 15min.
        builder.setPeriodic(15 * 60 * 1000);

        // Optional: Set additional requirements under what conditions your job should be triggered
        // builder.setRequiresCharging(true);
        builder.setOverrideDeadline(0);


        const jobScheduler = context.getSystemService(android.content.Context.JOB_SCHEDULER_SERVICE);
        console.log("Job Scheduled: " + jobScheduler.schedule(builder.build()));

}

module.exports.scheduleJob = scheduleJob;

myJobService.js

android.app.job.JobService.extend("com.tns.notifications.MyJobService", {
    onStartJob: function(params) {  
        console.log("Job execution ...");
        // Do something useful here, fetch data and show notification for example
        var utils = require("utils/utils");
        var context = utils.ad.getApplicationContext();

        var res;
        var geoService = require("./geocalc");
        var PointModell = require("./PointModell")

        nearby();
        async function nearby(){
            res=await geoService.geocalc();
            console.log("res",res); 
        }

            var builder = new android.app.Notification.Builder(context);
            console.log("setting notification head and body")
            builder.setContentTitle("you are in .......")
            .setAutoCancel(true)
            .setColor(android.R.color.holo_purple)//getResources().getColor(R.color.colorAccent))
            .setContentText("This notification has been triggered ")
            .setVibrate([100, 200, 100])
            .setSmallIcon(android.R.drawable.btn_star_big_on);



            // will open main NativeScript activity when the notification is pressed
            var mainIntent = new android.content.Intent(context, com.tns.NativeScriptActivity.class); 

            var mNotificationManager = context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);

            // The id of the channel.
            const channelId = "my_channel_01";
            // The user-visible name of the channel.
            const name = "Channel name";
            // The user-visible description of the channel.
            const description = "Channel description";
            const importance = android.app.NotificationManager.IMPORTANCE_LOW;
            if (android.os.Build.VERSION.SDK_INT >= 26) {
                console.log("apilevel is good",android.os.Build.VERSION.SDK_INT)
            }
            const mChannel = new android.app.NotificationChannel(channelId, name,importance);


            // const mChannel = new android.app.NotificationChannel(channelId, name,importance);
            // Configure the notification channel.
            mChannel.setDescription(description);
            mChannel.enableLights(true);
            // Sets the notification light color for notifications posted to this
            // channel, if the device supports this feature.
            mChannel.setLightColor(android.graphics.Color.RED);
            mChannel.enableVibration(true);
            mNotificationManager.createNotificationChannel(mChannel);

            builder.setChannelId(channelId);

            mNotificationManager.notify(1, builder.build());

            console.log("returning job ..."); 

            return false
        },

        onStopJob: function() {
            console.log("Stopping job ...");

        }

    });

【问题讨论】:

  • 您不能延迟退货。异步函数立即返回承诺。你仍然需要编写处理异步位的代码——async await 只是让它变得更好一点,它不会让问题消失。

标签: javascript promise nativescript job-scheduling


【解决方案1】:

即使使用 async/await,也不能延迟返回值。我认为你应该在处理完所有数据后返回true 并调用jobFinished 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-25
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2015-11-06
    • 1970-01-01
    • 2018-04-06
    相关资源
    最近更新 更多