Hi,
Last week we started to integrate ClassKit in our eductional apps for schools in The Netherlands. Despite the documentation is quite hard to understand the approach (especially the Apple team is expecting everything is nowdays written in Swift instead Obj C). But finally we got everything working fine except one thing:
How to setup a simple hierarchy in the CLSContext. The example on the video does not present a hierarchy and the example code in swift is not really setup for standard apps. Maybe we missed something in de docu and somebody can help us with the problem:
We would like to setup a chapter with an exercise. So the teacher hit our app icon, select the chapter and select finally one of the exercises. The setup of the model is totally static so we created from the documentations and demo the following function for setting up the activity model:
func publishContextIfNeeded(completion:((Error?) -> Void)? = nil) {
if #available(iOS 11.4, *) {
let chapter1 = CLSContext(type:.chapter, identifier: "chapter1", title:"Chapter 1")
chapter1.displayOrder = 0
let opdracht1 = CLSContext(type:.exercise, identifier: "K001001a", title:"Exercise 1: Mulitplications by 1")
opdracht1.displayOrder = 0
var contextToCreate: [String: CLSContext] = [
chapter1.identifier : chapter1]
var contextToCreate2: [String: CLSContext] = [
chapter1.identifier : chapter1,
opdracht1.identifier : opdracht1]
//get the parent context
let parent = CLSDataStore.shared.mainAppContext
//Issue a query by using CLSDataStore context matching identifier path
let predicate = NSPredicate(format: "parent = %@",parent)
CLSDataStore.shared.contexts(matching: predicate) { contexts, error in
//Check if we already have a context created and remove if from our context array needing to be created
for context in contexts {
contextToCreate.removeValue(forKey: context.identifier)
contextToCreate2.removeValue(forKey: context.identifier)
}
//Go through all of the contexts that have not yet been created and add them as a child to our parent
for (_, context) in contextToCreate {
parent.addChildContext(context)
}
//Go through all of the contexts that have not yet been created and add them as a child to our parent
for (_, context) in contextToCreate2 {
parent.addChildContext(context)
}
//Save our changes
CLSDataStore.shared.save { (error) in completion?(error)
if let error = error {
print(error.localizedDescription)
}
}
}
} else {
// Fallback on earlier versions
}
}
This code is working but the hierachy is missing in SchoolWork app and it presents everything on the same level. Hopefully someone can help us, we tried many ideas but none are working.
Kees