Trying to Update Calendar, Getting Various Errors - ios

I can add a new calendar to the user's calendars like this, using the saveCalendar(_:commit:) method:
let ekStore = EKEventStore()
func saveCalendar(calendar: EKCalendar) throws {
try ekStore.saveCalendar(calendar, commit: true)
}
and
let newList = EKCalendar(for: .reminder, eventStore: ekStore)
newList.source = ekStore.defaultCalendarForNewReminders()?.source
newList.title = newListName
newList.cgColor = listColor
do {
try saveCalendar(calendar: newList)
} catch {
print("Error adding list: \(error.localizedDescription)")
}
I then store the calendar object.
When the user finishes editing a calendar (reminders list) in my app, I try to save it like this, using the stored calendar as a starting point:
let updatedList = existingCalendar
updatedList.title = newListName
updatedList.cgColor = listColor
do {
try saveCalendar(calendar: updatedList)
} catch {
print("Error saving list: \(error.localizedDescription)")
}
But the calendar doesn't save and I get this error: That account does not support reminders..
I have also tried explicitly setting the calendar's source:
updatedList.source = ekStore.defaultCalendarForNewReminders()?.source
but then I get this error: That calendar may not be moved to another account..
My Question: How do I update calendars (reminder lists) from my app?

Related

Listener event not triggered when document is updated (Google Firestore)

I am struggling to understand why my event listener that I initialize on a document is not being triggered whenever I update the document within the app in a different UIViewController. If I update it manually in Google firebase console, the listener event gets triggered successfully. I am 100% updating the correct document too because I see it get updated when I update it in the app. What I am trying to accomplish is have a running listener on the current user that is logged in and all of their fields so i can just use 1 global singleton variable throughout my app and it will always be up to date with their most current fields (name, last name, profile pic, bio, etc.). One thing I noticed is when i use setData instead of updateData, the listener event gets triggered. For some reason it doesn't with updateData. But i don't want to use setData because it will wipe all the other fields as if it is a new doc. Is there something else I should be doing?
Below is the code that initializes the Listener at the very beginning of the app after the user logs in.
static func InitalizeWhistleListener() {
let currentUser = Auth.auth().currentUser?.uid
let userDocRef = Firestore.firestore().collection("users").document(currentUser!)
WhistleListener.shared.listener = userDocRef.addSnapshotListener { documentSnapshot, error in
guard let document = documentSnapshot else {
print("Error fetching document: \(error!)")
return
}
guard let data = document.data() else {
print("Document data was empty.")
return
}
print("INSIDE LISTENER")
}
}
Below is the code that update's this same document in a different view controller whenever the user updates their profile pic
func uploadProfilePicture(_ image: UIImage) {
guard let uid = currentUser!.UID else { return }
let filePath = "user/\(uid).jpg"
let storageRef = Storage.storage().reference().child(filePath)
guard let imageData = image.jpegData(compressionQuality: 0.75) else { return }
storageRef.putData(imageData) { metadata, error in
if error == nil && metadata != nil {
self.userProfileDoc!.updateData([
"profilePicURL": filePath
]) { err in
if let err = err {
print("Error updating document: \(err)")
} else {
print("Document successfully updated")
}
}
}
}
}
You can use set data with merge true it doesn't wipe any other property only merge to specific one that you declared as like I am only update the name of the user without wiping the age or address
db.collection("User")
.document(id)
.setData(["name":"Zeeshan"],merge: true)
The answer is pretty obvious (and sad at the same time). I was constantly updating the filepath to be the user's UID therefore, it would always be the same and the snapshot wouldn't recognize a difference in the update. It had been some time since I had looked at this code so i forgot this is what it was doing. I was looking past this and simply thinking an update (no matter if it was different from the last or not) would trigger an event. That is not the case! So what I did was append an additional UUID to the user's UID so that it changed.

CloudKit, after reinstalling an app how do I reset my subscriptions to current status of records?

I'm dealing with the scenario, where a user has previously deleted the app and has now re-installed it.
It was hitting my delta fetch function, which is receiving a lot of old subscription notifications, mostly deletes. But not downloading current records.
I'm now adding code to perform a fetch on each record type to download all the data.
I'd like to reset delta fetch server token, so the app doesn't have to process old subscriptions notifications. However I can't find how to do this, maybe it's not possible.
Are you referring to CKServerChangeToken (documentation) when you say "delta fetch server token"? And are you attempting to sync within the CloudKit private database?
Assuming that is true, here is an example of how I fetch changes from the private database and keep track of the sync token:
//MARK: Fetch Latest from CloudKit from private DB
func fetchPrivateCloudKitChanges(){
print("Fetching private changes...")
//:::
let privateZoneId = CKRecordZone.ID(zoneName: CloudKit.zoneName, ownerName: CKCurrentUserDefaultName)
/----
let options = CKFetchRecordZoneChangesOperation.ZoneOptions()
options.previousServerChangeToken = previousChangeToken
let operation = CKFetchRecordZoneChangesOperation(recordZoneIDs: [privateZoneId], optionsByRecordZoneID: [recordZoneID:options])
//Queue up the updated records to process below
var records = [CKRecord]()
operation.recordChangedBlock = { record in
records.append(record)
}
operation.recordWithIDWasDeletedBlock = { recordId, type in
//Process a deleted record in your local database...
}
operation.recordZoneChangeTokensUpdatedBlock = { (zoneId, token, data) in
// Save new zone change token to disk
previousChangeToken = token
}
operation.recordZoneFetchCompletionBlock = { zoneId, token, _, _, error in
if let error = error {
print(error)
}
// Write this new zone change token to disk
previousChangeToken = token
}
operation.fetchRecordZoneChangesCompletionBlock = { error in
if let error = error {
print(error
}else{
//Success! Process all downloaded records from `records` array above...
//records...
}
}
CloudKit.privateDB.add(operation)
}
//Change token property that gets saved and retrieved from UserDefaults
var previousChangeToken: CKServerChangeToken? {
get {
guard let tokenData = defaults.object(forKey: "previousChangeToken") as? Data else { return nil }
return NSKeyedUnarchiver.unarchiveObject(with: tokenData) as? CKServerChangeToken
}
set {
guard let newValue = newValue else {
defaults.removeObject(forKey: "previousChangeToken")
return
}
let data = NSKeyedArchiver.archivedData(withRootObject: newValue)
defaults.set(data, forKey: "previousChangeToken")
}
}
Your specific situation might differ a little, but I think this is how it's generally supposed to work when it comes to staying in sync with CloudKit.
Update
You could try storing the previousServerChangeToken on the Users record in CloudKit (you would have to add it as a new field). Each time the previousServerChangeToken changes in recordZoneFetchCompletionBlock you would have to save it back to iCloud on the user's record.

CloudKit Sharing

I am having trouble understanding some of the CloudKit sharing concepts and the WWDC 2016 "What's new in CloudKit" video doesn't appear to explain everything that is required to allow users to share and access shared records.
I have successfully created an app that allows the user to create and edit a record in their private database.
I have also been able to create a Share record and share this using the provided sharing UIController. This can be successfully received and accepted by the participant user but I can't figure out how to query and display this shared record.
The app creates a "MainZone" in the users private database and then creates a CKRecord in this "MainZone". I then create and save a CKShare record and use this to display the UICloudSharingController.
How do I query the sharedDatabase in order to access this record ? I have tried using the same query as is used in the privateDatabase but get the following error:
"ShareDB can't be used to access local zone"
EDIT
I found the problem - I needed to process the accepted records in the AppDelegate. Now they appear in the CloudKit dashboard but I am still unable to query them. It seems I may need to fetch the sharedDatabase "MainZone" in order to query them.
Dude, I got it: First you need to get the CKRecordZone of that Shared Record. You do it by doing the following:
let sharedData = CKContainer.default().sharedCloudDatabase
sharedData.fetchAllRecordZones { (recordZone, error) in
if error != nil {
print(error?.localizedDescription)
}
if let recordZones = recordZone {
// Here you'll have an array of CKRecordZone that is in your SharedDB!
}
}
Now, with that array in hand, all you have to do is fetch normally:
func showData(id: CKRecordZoneID) {
ctUsers = [CKRecord]()
let sharedData = CKContainer.default().sharedCloudDatabase
let predicate = NSPredicate(format: "TRUEPREDICATE")
let query = CKQuery(recordType: "Elder", predicate: predicate)
sharedData.perform(query, inZoneWith: id) { results, error in
if let error = error {
DispatchQueue.main.async {
print("Cloud Query Error - Fetch Establishments: \(error)")
}
return
}
if let users = results {
print(results)
self.ctUsers = users
print("\nHow many shares in cloud: \(self.ctUsers.count)\n")
if self.ctUsers.count != 0 {
// Here you'll your Shared CKRecords!
}
else {
print("No shares in SharedDB\n")
}
}
}
}
I didn't understand quite well when you want to get those informations. I'm with the same problem as you, but I only can get the shared data by clicking the URL... To do that you'll need two functions. First one in AppDelegate:
func application(_ application: UIApplication, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShareMetadata) {
let acceptSharesOperation = CKAcceptSharesOperation(shareMetadatas: [cloudKitShareMetadata])
acceptSharesOperation.perShareCompletionBlock = {
metadata, share, error in
if error != nil {
print(error?.localizedDescription)
} else {
let viewController: ViewController = self.window?.rootViewController as! ViewController
viewController.fetchShare(cloudKitShareMetadata)
}
}
CKContainer(identifier: cloudKitShareMetadata.containerIdentifier).add(acceptSharesOperation)
}
in ViewConroller you have the function that will fetch this MetaData:
func fetchShare(_ metadata: CKShareMetadata) {
let operation = CKFetchRecordsOperation(recordIDs: [metadata.rootRecordID])
operation.perRecordCompletionBlock = { record, _, error in
if error != nil {
print(error?.localizedDescription)
}
if record != nil {
DispatchQueue.main.async() {
self.currentRecord = record
//now you have your Shared Record
}
}
}
operation.fetchRecordsCompletionBlock = { _, error in
if error != nil {
print(error?.localizedDescription)
}
}
CKContainer.default().sharedCloudDatabase.add(operation)
}
As I said before, I'm now trying to fetch the ShareDB without accessing the URL. I don't want to depend on the link once I already accepted the share. Hope this helps you!

how to create a location field in cloud kit

I'm trying to save a CKRecord with a location field along with it but when ever I try to it crashes. Here's the code I'm using (crashes where it says: // CRASHES HERE):
func saveStateMood(stateToSave:String) {
// Create CK record
let newRecord:CKRecord = CKRecord(recordType: "State")
let newLocation:CKLocationSortDescriptor = CKLocationSortDescriptor(key: "Loco", relativeLocation: self.theState)
newRecord.setValue(stateToSave, forKey: "State")
newRecord.setValue(newLocation, forKey: "Loco") // CRASHES HERE!!!!!!
// Save record into public database
if let database = self.publicDatabase {
database.saveRecord(newRecord, completionHandler: { (record:CKRecord!, error:NSError!) -> Void in
// Check for error
if error != nil {
// There was an error
NSLog(error.localizedDescription)
}
else {
// There was no error
dispatch_async(dispatch_get_main_queue()) {
// Refresh table
//self.retrieveStateMoods("")
}
}
})
}
}
(I'm using the CLGeocoder to find the current location then I assign the current location to "theState")
I'm trying to follow the CloudKit Quick Start guideline for adding location fields but it is all written in Obj-C and I can't seem to figure it out and I also can't figure out how to fetch the records by their location field either.
You need to store an instance of CLLocation in your record. But you are trying to save an instance of CKLocationSortDescriptor. Update your code to use CLLocation.

Error getting new calendar for new reminders

I'm trying to add a feature in my application so the user can get a list of reminders using the following method.
The following method is the main method I'm using for retrieving the reminders:
func getReminders(){
var eventStore : EKEventStore = EKEventStore()
// This lists every reminder
var calender = getCalender(eventStore)
let calendars = eventStore.calendarsForEntityType(EKEntityTypeReminder)
as! [EKCalendar]
//cals.append(calender)
var predicate = eventStore.predicateForRemindersInCalendars([calender])
eventStore.fetchRemindersMatchingPredicate(predicate) { reminders in
for reminder in reminders {
println(reminder.title)
self.remindersTitles.append(reminder.title!!)
}}
var startDate=NSDate().dateByAddingTimeInterval(-60*60*24)
var endDate=NSDate().dateByAddingTimeInterval(60*60*24*3)
var predicate2 = eventStore.predicateForEventsWithStartDate(startDate, endDate: endDate, calendars: nil)
println("startDate:\(startDate) endDate:\(endDate)")
var eV = eventStore.eventsMatchingPredicate(predicate2) as! [EKEvent]!
if eV != nil {
for i in eV {
println("Title \(i.title!)" )
println("stareDate: \(i.startDate)" )
println("endDate: \(i.endDate)" )
}
}
}
As you notice I'm creating a calendar and assign it the return value of a method called 'getCalender':
func getCalender(store: EKEventStore) -> EKCalendar {
let defaults = NSUserDefaults.standardUserDefaults()
if let id = defaults.stringForKey("GSCalender") {
return store.calendarWithIdentifier(id)
} else {
var calender = EKCalendar(forEntityType: EKEntityTypeReminder, eventStore: store)
calender.title = "Genie Sugar Calender!"
calender.CGColor = UIColor.redColor().CGColor
calender.source = store.defaultCalendarForNewReminders().source!
var error: NSError?
store.saveCalendar(calender, commit: true, error: &error)
if error == nil {
defaults.setObject(calender.calendarIdentifier, forKey: "GSCalender")
}
if calender == nil {
println("nothing here")
}
return calender
}
}
But the issue is that the application is stuck at this line of the second method:
calender.source = store.defaultCalendarForNewReminders().source!
And returns me this error:
Error getting default calendar for new reminders: Error Domain=EKCADErrorDomain Code=1013 "The operation couldn’t be completed.
Any ideas please to overcome this problem? with my advanced thanks
I noticed the iPhone simulator, after it being reset - returns nil for store.defaultCalendarForNewReminders(). I believe it is a simulator bug.
By doing lots of tests, I came up with an observation that the simulators can fetch defaultCalendarForNewReminders() smoothly (with calendar usage permission)....but with the testing on a real device with iOS 14.6 it is returning nil, even an iPhone has the default calendar for events already!
I had also tried with EKCalendarChooser in order to select other calendars, but still it not worked!
So I think it is an iOS bug.

Resources