I want to Run a Service all the time Even App is c

2019-09-17 07:02发布

Here is sample code service that i want to run all the time

internal func backService()
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { () -> Void in
            let anObject = try! Realm().objects(remindertbl).filter("status = 0")
            print("here")
            for loop in anObject
            {
                let dateFormatter = NSDateFormatter()
                dateFormatter.dateFormat = "dd/MM/yyyy hh:mm a"
                let  timestamp = dateFormatter.stringFromDate(NSDate())
                let timeStampint = dateFormatter.dateFromString(timestamp)!.timeIntervalSince1970
                if(loop.reminderdays == Int64(timeStampint))
                {
                    if(loop.status == 0){                            

                    let notification = UILocalNotification()
                    notification.fireDate = NSDate(timeIntervalSince1970: 0)
                    notification.alertBody = "\(loop.title)"
                    notification.alertAction = "swipe to opem VLB Cloud!"
                    notification.soundName = UILocalNotificationDefaultSoundName
                    notification.userInfo = ["CustomField1": "w00t"]
                    UIApplication.sharedApplication().scheduleLocalNotification(notification)

                    let updateObject = try! Realm().objects(remindertbl).filter("status = 0")
                    var reminderObject = remindertbl()
                    for updateStatus in updateObject{
                        try! Realm().write()
                            {
                                print("target")
                                reminderObject.status = 2
                                reminderObject.reminderId = updateStatus.reminderId
                                reminderObject.syncflag = updateStatus.syncflag
                                reminderObject.vehicleid = updateStatus.vehicleid
                                reminderObject.syncid = updateStatus.syncid
                                reminderObject.dateadded = updateStatus.dateadded
                                reminderObject.datemodified = Int64(Timestamp)!
                                reminderObject.remindertime = updateStatus.remindertime
                                reminderObject.reminerDate = updateStatus.reminerDate
                                reminderObject.reminderdays = updateStatus.reminderdays
                                reminderObject.title = updateStatus.title
                                reminderObject.targetdistance = updateStatus.targetdistance
                                reminderObject.currentdistance = updateStatus.currentdistance
                                reminderObject.remindertype = updateStatus.remindertype

                                reminderObject.synctype = updateStatus.synctype
                                reminderObject.recuringdays = updateStatus.recuringdays
                                reminderObject.recuringdistance = updateStatus.recuringdistance

                                try! Realm().add(reminderObject, update: true)
                                reminderObject = remindertbl()
                        }
                    }
                }
            }
        }

    })
}

1条回答
萌系小妹纸
2楼-- · 2019-09-17 07:21

What you are asking is not possible for multiple reasons:

  1. A task/app that has been killed or terminated cannot by definition run some code
  2. A few background modes are atuhorized by Apple on iOS devices, if you are not specific task such as voIP, your app won't be authorized to run at all times by the OS

If you want to learn more about executing some tasks on the background on iOS, I suggest you read the official documentation: Apple Documentation or this tutorial by the Ray Wenderlich team: Background modes for iOS

查看更多
登录 后发表回答