Hi everyone and thank you for be reading this.
I am new with Realm and I am stucked with a function that I can't figure out how to build. My intention is to have one function called func delete(className:String, id:Int){ that should be able to delete any object of any Realm class by its ID. The inner code of the function is:
func delete(objectNameV:String, id:Int){
let theClass = NSClassFromString(objectNameV)
// Get the default Realm
let realm = try! Realm()
let queryResult = realm.objects(theClass as! Object.Type).filter("id = \(id)")
try! realm.write {
realm.delete(queryResult)
}
}
But the fact is that let theClass = NSClassFromString(objectNameV) it's allways NIL.
Any help is appreciated, i just need a function that given a realm classname and the id from an object of that class can delete it!
Realm version: 2.0.1
Xcode version: 8
According to the Apple developer docs, classes in Swift are namespaced by their module names. So it's not sufficient to simply write the class name; you must also include their module name as well.
The example Apple used on their website is:
let myPersonClass: AnyClass? = NSClassFromString("MyGreatApp.Person")
Related
The documentation states the obvious i.e.:
add(_:update:) Adds or updates an object to be persisted it in this Realm.
create(_:value:update:) Creates or updates an instance of this object and adds it to the Realm populating the object with the given value.
but I can't quite see the difference if any ?
add will update whole object at once, so there is a danger that you may miss an attribute. create can update partial information of the object by displaying attribute names.
Let's say cheeseBook is saved already as below.
let cheeseBook = Book()
cheeseBook.title = "cheese recipes"
cheeseBook.price = 9000
cheeseBook.id = 1
//update 1 - title will be empty
try! realm.write {
let cheeseBook = Book()
cheeseBook.price = 300
cheeseBook.id = 1
realm.add(cheeseBook, update:true)
}
//update2 - title will stay as cheese recipes
try! realm.write {
realm.create(Book.self, value:["id":1, "price":300], update:true)
}
add() adds the passed-in object to the Realm (modifying the object to now refer to the data in the Realm), while create() creates a copy of the object in the Realm and returns that copy, and does not modify the argument.
If some function or library does not have DefinitelyTyped, I know these two ways to stop warnings.
interface Navigator {
getUserMedia: any
}
declare let RTCIceCandidate: any;
But right now, this 3rd-part library Collection2 is used like this:
let ProductSchema = {};
let Products = new Mongo.Collection('products');
Products.attachSchema(ProductSchema);
It give me a warning:
Property 'attachSchema' does not exist on type 'Collection'.
I tried the way below, but it does not work.
interface Collection {
attachSchema: any
}
How can I stop this warning? Thanks
EDIT:
Eric's adding any way solves the problem.
let Products:any = new Mongo.Collection('products');
Products.attachSchema(ProductSchema);
But now a new trouble comes:
let UserSchema = {};
Meteor.users.attachSchema(UserSchema);
Since Meteor.users is built in, so there is no place to add any. How to solve this? Thanks
Thanks for Amid's help. So the way is:
(<any>Meteor.users).attachSchema(UserSchema);
I want to share a document with JavaScript and get its share_id programatically.
There is a REST API that can do that but I didn't know how to call it from script.
Any clues?
The following hack will do the trick. (edit: Must be executed from the classpath in the repository)
var ctx = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
var qsService = ctx.getBean("QuickShareService");
var sId = document.properties['qshare:sharedId'];
if (!sId) {
sId = qsService.shareContent(document.nodeRef).id;
}
PS: It looks even more ugly on 5.0.a due to rhino-1.7.
What I'm doing NOW:
Often multiple instances of the view component would be used in multiple places in an application. Each time I do this, I register the same mediator with a different name.
When a notification is dispatched, I attach the name of the mediator to the body of the notification, like so:
var obj:Object = new Object();
obj.mediatorName = this.getMediatorName();
obj.someParameter = someParameter;
sendNotification ("someNotification", obj);
Then in the Command class, I parse the notification body and store the mediatorName in the proxy.
var mediatorName:String = notification.getBody().mediatorName;
var params:String = notification.getBody().someParameter;
getProxy().someMethod(params, mediatorName);
On the return notification, the mediatorName is returned with it.
var obj:Object = new Object();
obj.mediatorName = mediatorName;
obj.someReturnedValue= someReturnedValue;
sendNotification ("someReturnedNotification", obj);
In the multiple mediators that might be watching for "someReturnedNotification," in the handleNotification(), it does an if statement, to see
if obj.mediatorName == this.getMediatorName
returns true. If so, process the info, if not, don't.
My Question is:
Is this the right way of using Multiton PureMVC? My gut feeling is not. I am sure there's a better way of architecting the application so that I don't have to test for the mediator's name to see if the component should be updated with the returned info.
Would someone please help and give me some direction as to what is a better way?
Thanks.
I checked with Cliff (the puremvc.org guy) and he said it's fine.
I have the following code in actionscript 3:
var async:AsyncToken;
async = bridge.retornamenu();
The bridge is a remote object, instantiated.
The retornamenu() is the function that I want the remote object open in amfphp.
However the retornamenu() is a dynamic function, which turns another function, but I can not run it at runtime,
example
var stringfunction:String = "retornamenu()" // this name is dynamic.
var async:AsyncToken;
async = bridge.stringfunction;
But this way does not work, not perform the function retornamenu();
someone could help me?
I am a few days behind the solution, my project stopped,
Thanks in advance
Use getOperation() and send() it.
var stringfunction:String = "retornamenu" // this name is dynamic.
var async:AsyncToken;
async = bridge.getOperation(stringfunction).send();
If there are arguments to the function, you can pass it through send(args)