微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

地理围栏 didEnterRegion 不调用存储的 CoreData

如何解决地理围栏 didEnterRegion 不调用存储的 CoreData

我正在尝试使用 didEnterRegion 来安排通知,并使用 didExitRegion 根据存储在 CoreData 文件中的数据取消通知。它似乎无法访问 CoreData。我想根据人员位置安排通知

我该如何解决这个问题?

func locationManager(_ manager: CLLocationManager,didEnterRegion region: CLRegion) {
    
    let ident = region.identifier
    scheduleLocationNotification(ident: ident)
    
}

func scheduleLocationNotification(ident: String) {
    let count = reminderArray.count
    print("Started Scheduled Geo")
    
    for i in 0...count {
        
        let reminder = reminderArray?[i]
        let id = reminder?.identity
        
        let date = reminderArray?[i].time ?? Date()
        var time: String = ""

        let formatter = DateFormatter()
        formatter.timeStyle = .short
        formatter.dateStyle = .none
        
        if reminderArray?[i].time != nil {
            time = formatter.string(from: date)
        }

        if reminder?.active == true {
            if id == ident {
                scheduler.weekDaySchedule(time: time)
                print("GeoScheduled Notifications")
            }
        }
    }
}

func locationManager(_ manager: CLLocationManager,didExitRegion region: CLRegion) {
   
    let ident = region.identifier
    cancelLocationNotification(ident: ident)
    
}

func cancelLocationNotification(ident: String) {

    let count = reminderArray.count
    print("Started Cancel Geo")
    
    for i in 0...count {
        
        let reminder = reminderArray?[i]
        let id = reminder?.identity
                    
        if reminder?.active == true {
            if id == ident {
                for i in 1...7 {
                    scheduler.cancelNotifications(identity: id! + String(i))
                    print("GeoCanceled Notifications")
                }
            }
          }
    }
    
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。