Dear All,
I’m happy to be a new member of the community, this platform seems to have exactly what I’m planning for my app. Currently I’m trying to understand the how mongoldb works.
I started with the IOS tutorial of creating a TaskApp with the ability to have common project etc.
I made the app work as per the instructions, great!
Now following my future goals, I started to modify part of the project and mongobd backend, as follows:
Mongoldb:
Functions:
[modified] - createUserDocument - to make user able to change it own fields(to add new project)
exports = async function createNewUserDocument({user}) { const cluster = context.services.get("mongodb-atlas"); const users = cluster.db("tracker").collection("User"); return users.insertOne({ _id: user.id, _partition: `user=${user.id}`, name: user.data.email, canReadPartitions: [], canWritePartitions: [`user=${user.id}`,`project=${user.id}`], memberOf: [ {"name": "My Project", "partition": `project=${user.id}`} ], }); };
[added] - addWriteRulesOnNewProject
exports = async function(projectPartition) {
const collection = context.services.get(“mongodb-atlas”).db(“tracker”).collection(“User”);
const localUserID = context.user.id
if (localUserID == null) {
return {error:No userID found
};
}
if (collection == null) {
return {error:No user collection
};
}
if (projectPartition == null) {
return {error:No project partition
};
}try {
return await collection.updateOne({_id: localUserID}, {$addToSet: { canWritePartitions: projectPartition, canReadPartitions: projectPartition }} );
} catch (error) {
return {error: error.toString()};
}};
Swift Tutorial TaskProject
[added] - in ProjectViewController @ viewdidLoad a right item “add”
// NEW//////////////
navigationItem.rightBarButtonItem = UIBarButtonItem(title: “Add”, style: .plain, target: self , action: #selector (addProject))
[added] - in ProjectViewController - function addProject
@objc func addProject(){
try! userRealm.write() { let project = Project(partition: "project=\(ObjectId.generate().stringValue)", name: "New Project created") guard let user = self.userRealm.object(ofType: User.self, forPrimaryKey: app.currentUser?.id) else { print ("no users"); return} user.memberOf.append(project) //Add projectpartition to users canwrite and canread array addProjectRulesToUser(projectPartition: project.partition!) }
}
func addProjectRulesToUser(projectPartition: String) {
print(“Adding project: (projectPartition)”)
let user = app.currentUser!
user.functions.addProjectRules([AnyBSON(projectPartition)], self.onNewProjectOperationComplete)
}
Results:
- On new user a new user documented created as expected with all new write rules and when tapping add button, a new project added to members array and to the write rules
Issue:
- If tapping on the newly created project app crashes with:
Swift:
Error Domain=io.realm.unknown Code=89 “Operation canceled” UserInfo={Category=realm.basic_system, NSLocalizedDescription=Operation canceled, Error Code=89}: file Task_Tracker/ProjectsViewController.swift, line 153
MongodbLog:
mongo db log :
Now I stop the app, restart the app and tap again and it loads it, but if I create again a new one and tap it, it again crashes at the Realm.async when trying to download the realm locally, I guess.
What is wrong here?
Thank you!
I love the platform, cannot wait to deploy the real app!