Post not yet marked as solved
To be honest I’ve moved on from CloudKit. There were just too many cases where the paradigm didn’t work for my application.
Good luck getting the answers - 7 months on and I still can’t get the out of the box boilerplate code to run correctly without modification. Could be something I’ve got wrong in the old beta configs…
Post not yet marked as solved
Here's some base code (slightly modified boiler plate). At the moment, selecting the item just opens the detail view in the right column. I want the sidebar (left column) to slide away as soon as I select an item.
import CoreData
struct DetailView: View {
@Environment(\.managedObjectContext) private var viewContext
@ObservedObject var item: Item
func save() {
do {
try viewContext.save()
} catch {
var nserror = error as NSError
fatalError("Unresolved error: \(nserror)")
}
}
var body: some View {
Text("Item timestamp: \(item.timestamp!)")
Button(action: {
item.setElement(data: item.values!, row: 0, col: 2, val: item.getElement(data: item.values!, row: 0, col: 2) + 1)
save()
}) {
Text("Add one")
}
}
}
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
animation: .default)
private var items: FetchedResults<Item>
var body: some View {
NavigationView {
List {
ForEach(items) { item in
NavigationLink(destination: DetailView(item: item)) {
Text("Item at \(item.timestamp!, formatter: itemFormatter)")
}
}
.onDelete(perform: deleteItems)
}
.toolbar {
#if os(iOS)
ToolbarItem(placement: .bottomBar) {
EditButton()
}
#endif
ToolbarItem(placement: .primaryAction) {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
}
}
/*
let value: [Int16] = [1, Int16.max, Int16.min]
let data = Data(fromArray: value)
print(data as NSData) // <0100ff7f 0080>
let roundtrip = data.toArray(type: Int16.self)
print(roundtrip) // [1, 32767, -32768]
*/
private func addItem() {
withAnimation {
let newItem = Item(context: viewContext)
newItem.timestamp = Date()
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { items[$0] }.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
}
private let itemFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .medium
return formatter
}()
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}
Im still seeing the problem in Beta 5. AT first I thought it was working as one simulator was correctly connecting, but then running it on a second simulator (or on a real device) resulted in no updates to CloudKit from the new devices, and the auth token error came back.
Interestingly, the first simulator that ran the program continues to communicate correctly with iCloud.
Can someone please try the following to confirm it's not just me:
Create a new project with CoreData and CloudKit enabled
Make the boiler plate changes to correct the on-screen behaviour (the View body should be as below)
Set the deployment target to iOS 15
Add the background -> remote notifications capability
Add the iCloud -> CloudKit capability
Connect to an existing container (creating a new container seems to be troublesome, with CloudKit console unable to list the new zones)
Build and run on one simulator (log messages should indicate success)
Build and run on a second simulator (log messages may display "auth token" error
That's in advance to anyone who can confirm this.
Andy
Working boilerplate body here:
NavigationView{
List {
ForEach(items) { item in
Text("Item at \(item.timestamp!, formatter: itemFormatter)")
}
.onDelete(perform: deleteItems)
}
.toolbar {
#if os(iOS)
ToolbarItem(placement: .bottomBar) {
EditButton()
}
#endif
ToolbarItem(placement: .primaryAction) {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
}
Out of interest I've just confirmed that this error also occurs when I connect a physical iPad to the Mac and run the app there, so I don't believe it is a simulator error.
Seeing the same error - basic boiler plate code, so it's not program logic that's causing this.
Perfect - thank you that worked!
Just for interest, my path was a little different:
xcode-select -p
/Library/Developer/CommandLineTools
and then
sudo xcode-select -s /Applications/Xcode-beta.app
Post not yet marked as solved
Beta 3 seems to have resolved most of the issues, with Beta 4 fixing the problems with provisioning profiles... back to trying to get sharing to work...
If anyone has seen cktool in action please let me know - I can't find it anywhere in the beta installations :)
This has been resolved in Xcode 13 Beta 4.
Post not yet marked as solved
There is : https://developer.apple.com/documentation/coredata/synchronizing_a_local_store_to_the_cloud
Post not yet marked as solved
Interestingly still no fix in Beta 2, and not a lot of conversation. Am I the only one encountering this (I know that I'm not, but it does seem very quiet)
Has anyone been able to get this to work in Beta 2? Still looks broken to me...
Post not yet marked as solved
I'm still having these problems on Beta 2. Anyone else?
Post not yet marked as solved
OK. More experimenting and the plot thickens.
To check whether this was a simulator or Xcode issue, I deployed the modified base application to a local iPad and iPhone. Interestingly the user sync errors now go away, maying me believe there is something wrong with the sign-in capability on the Beta simulator...
However no record changes were observable in the CloudKit Dashboard. I tried signing out and in again without any change.
I then reset the dev environment in CloudKit, and restarted the apps on the devices.
At this stage, interestingly, they show some interesting behaviour.
The items previously created are gone - that makes sense, since I just blew away the development data
The Schema is not loaded, as expected for a refreshed Dev environment (i.e. the CD_Item record type doesn't exist)
When I add my first item in the app, the CD_Item record type appears in the schema
I then add an index on RecordID, and query the database, which yields no records.
So the app is definitely communicating successfully with CloudKit now, as it's building the schema, but the data is not appearing in the database.
And it gets more interesting again.
When I add an item on the iPhone, the iPad debug window seems to indicate it is getting push notifications from the database update, but those changes don't appear in the iPad interface...
And lastly, if I then try to delete an Item on the iPad, I get an error message that a merge conflict could not be resolved.
Has something become pretty seriously unravelled in this latest release?
Post not yet marked as solved
No such luck in WWDC2021...
I'm starting to see my namespace get out of control in the containers from scratch apps I'm building to figure out CK sharing...
Especially in the Development zone, this is really needed!
Im having exactly the same problem. So far no resolution. Definitely seems to be the beta, as I've build the "CoreData + CloudKit" boilerplate app in Xcode 12.x which works, and then built the same with Xcode 13 which breaks.
Did you also notice that when you go back to Xcode 12.x, the old "minimise to refresh" trick doesn't work anymore? You have to close the app and restart it for the iCloud data to get pulled.