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()
}
}
}
}
}
})
}
What you are asking is not possible for multiple reasons:
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