将事件批量保存到Google日历会导致随机事件丢失

时间:2015-02-23 00:19:39

标签: ios swift google-calendar-api batch-processing eventkit

我有一大堆活动要批量保存到日历中。用户选择了哪个日历。他们可以选择导出到iCloud日历或Google日历。导出到iCloud日历时,一切运行顺利。没问题。但是,在导出到Google日历时,我遇到了一些奇怪的问题。要保存的事件数量约为60-90个事件。我使用下面提供的功能在后台导出日历事件。操作运行正常,在记录期间包含所有事件,当迭代事件时,他们都收到了eventIdentifier。 但是,在任何情况下,大约有5-10个事件未同步到Google日历,并且未显示在电话日历上。未显示的事件对于每个导出都是不同的,因此事件本身不是错误的。我尝试了很多不同的方法,但没有成功。 我尝试过的: - 删除了后台操作。 - 删除了日历状态回调。 - 将函数移到闭包之外并直接调用它。 - 删除了@autorelease。 - 检查整个操作过程中EKEventStore和EKCalendar是否存在。

你们中有谁知道这方面的好解释吗?我检查了谷歌是否对保存有任何限制,但根据文件,在短时间内导入10 000多个事件时,日历可能会变为只读,我甚至都不接近。

我会喜欢任何反馈。这真让我抓狂。正如我之前所说,iCloud导出工作正常。

这是我的导出代码:

import UIKit
import EventKit

struct Activity {
   var title : String!
   var startDate : NSDate!
   var endDate : NSDate!
}

class CalendarManager: NSObject {

class func saveToCalendarInBackground(activities: [Activity], eventStore: EKEventStore, calendar: EKCalendar, calendarStatus: (status: String!, progress: Float!) -> (), completion:(success: Bool!, error: NSError!) -> ()) -> Void {

    //Run the operations on another thread (not main), but do UI updates on the main thread
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

        if activities.count > 0 {

            var formatter = NSDateFormatter()

            autoreleasepool {

                //Save duties to calendar
                for (index, activity) in enumerate(activities) {

                    //Update status
                    let progress = Float(index + 1) / Float(activities.count)

                    //Return callbacks on main thread
                    dispatch_sync(dispatch_get_main_queue(), {
                        calendarStatus(status: "Saving \(index+1) of \(activities.count)", progress: progress)
                    })

                    //Save activity
                    var event = EKEvent(eventStore: eventStore)
                    event.calendar = calendar
                    event.title = activity.title
                    event.startDate = activity.startDate
                    event.endDate = activity.endDate

                    var saveEventError : NSError?

                    if eventStore.saveEvent(event, span: EKSpanThisEvent, commit: false, error: &saveEventError) {

                        println("Activity saved. Commit needed.")

                    }
                    else {
                        println("Save error: \(saveEventError?.localizedDescription)")
                    }

                    //
                }

            }

            //Save all pending events
            var saveAllEventsError : NSError?

            if eventStore.commit(&saveAllEventsError) == true{
                println("Save all events complete!")

                //Return callbacks on main thread
                dispatch_async(dispatch_get_main_queue(), {
                    println("Calendar Save completion.")

                    calendarStatus(status: "Calendar save complete!", progress: 1)

                    completion(success: true, error: nil)
                })
                return
            }
            else {

                //Return callbacks on main thread
                dispatch_async(dispatch_get_main_queue(), {
                    completion(success: false, error: NSError(domain: "Calendar Save Error", code: 0, userInfo: [NSLocalizedFailureReasonErrorKey : "Error batch saving events: \(saveAllEventsError?.localizedDescription)"]))
                })

                println("Save all events ERROR: \(saveAllEventsError?.localizedDescription)")
                return
            }
        }
        else {

            //Return callbacks on main thread
            dispatch_async(dispatch_get_main_queue(), {
                completion(success: false, error: NSError(domain: "Calendar Save Error", code: 0, userInfo: [NSLocalizedFailureReasonErrorKey : "Found no events to save!"]))
            })
            return
        }
    })
}

}

0 个答案:

没有答案