How to include all keys in single query to Parse object - parse-platform

var query = new Parse.Query("Order");
query.equalTo("objectId", orederId);
query.include("price");
...
query.include("keyName");
Is it possible in Parse to include all existing object's keys in single query if we don't know keys names?
Assume that object was created dynamically and there is no info about it's keys names, but we need to get this object with all included pointers.

Create a subclass for your object and override the query method like this. Put all includeKeys where the comment says. Use the class method initObject to create and initialize your objects.
#implementation MyPFObject
#dynamic objectUUID;
// Other fields
+ (MyPFObject*) initObject
{
MyPFObject* obj = [self object];
[obj tlObjectInit];
// Other initializations
return obj;
}
+ (NSString*) parseClassName
{
return #"MyPFObject";
}
+ (PFQuery*) query
{
PFQuery* query = [PFQuery queryWithClassName: [self parseClassName]];
// Add includeKeys here
return query;
}
#end

Related

NSMutableArray initialize and destroy values

i'm creating an Xcode app and i need to create an NSMutableArray that gets initialized when my switch are on and when a switch is off the value assigned to the switch that was also assigned to the NSMutableArray gets destroyed from the Array, how the code should be? I already initialized my switch but i don't know how to add/destroy the values from the NSMUtableArray
- (void)changeState:(id)sender
{
UICustomSwitch* currentSwitch = (UICustomSwitch*)sender;
if ([currentSwitch isOn]) {
[myMutableArray addObject:currentSwitch.myCustomValue];
} else {
[myMutableArray removeObject:currentSwitch.myCustomValue];
}
}
if you have to add new object in NSMutableArray
ringsArray.addObject(_ring)
if insert object within NSMutableArray
ringsArray.insertObject("Value", atIndex: position)
Same as work on remove value
ringsArray.removeObject("")
ringsArray.removeObjectAtIndex(position)

Change default NSManagedObjectContext of NSPersistentDocument

Core data newbie here. I'm trying to change the default NSManagedObjectContext of an NSPersistentDocument, in order to initialise and use it with NSMainQueueConcurrencyType.
Currently I'm doing it in -windowControllerDidLoadNib: like this:
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
NSManagedObjectContext *newMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[newMOC setPersistentStoreCoordinator:[self.managedObjectContext persistentStoreCoordinator]];
[self setManagedObjectContext:newMOC];
}
This seemingly works fine. But I'm wondering if initialisation of the MOC in -windowControllerDidLoadNib: is the best thing to do, or whether it should be placed somewhere else and/or initialised in a different way.
Thanks for any help.
I'm experimenting with the Xcode template for a document-based CoreData app. The template creates an init() override which just calls super.init(). I want to run a large import in the background, so I added this to the document class:
class Document: NSPersistentDocument {
private var importQueue = DispatchQueue(label: "Importer")
override init() {
super.init()
let moc = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
moc.mergePolicy = self.managedObjectContext!.mergePolicy
moc.persistentStoreCoordinator = self.managedObjectContext!.persistentStoreCoordinator
self.managedObjectContext = moc
}
func importStuff(url: URL) {
let moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
moc.parent = self.managedObjectContext
var count = 0
moc.performAndWait {
...
count += 1
if count % 10000 == 0 {
do {
try moc.save()
moc.reset()
}
catch {
Swift.print("save failed at record #\(count): \(error.localizedDescription)")
}
}
return true
}
do {
try moc.save()
}
catch {
Swift.print("save failed at records #\(count): \(error.localizedDescription)")
}
}
Swift.print("imported \(count) records.")
}
#IBAction func import(_ sender: Any) {
...
importQueue.async {
self.importStuff(url: url)
}
}
}
This seems to work OK in my initial tests. I think initializing a new MOC in -windowControllerDidLoadNib: is OK, but if you have object controllers bound to the document MOC, they might perform a second fetch when the MOC is changed. Initializing it in the init will initialize it sooner, before the UI is loaded.
The preferred pattern is the "pass-the-baton" approach where you pass the managed object context down to the child view controllers. Give your controllers a managed object context attribute and simply pass it on when you present them.

Copy/Paste of a custom managed object in core data

I am having a very hard time getting copy/paste to work on my custom managed object Person. The object contains properties and relationships. The object should provide the objectID. I intend to implement pasting generating a new object and then filling in the information from the copied Person.
Copying the objectID probably does work. I am certain that pasting does not work. I have the following methods implemented in my Person class, in an attempt to copy/paste an object:
#pragma mark --- Copy functionality
-(id)pasteboardPropertyListForType:(NSString *)type
{
if ( [type isEqualToString:#"my.company.person"])
{
NSManagedObjectID *oid = self.objectID;
NSURL *uidURL = [oid URIRepresentation];
return [uidURL absoluteString];
}
return nil;
}
-(NSArray *)writableTypesForPasteboard:(NSPasteboard *)pasteboard
{
return #[#"my.company.person"];
}
+ (NSPasteboardWritingOptions)writingOptionsForType:(NSString *)type pasteboard:(NSPasteboard *)pasteboard
{
if ( [type isEqualToString:#"my.company.person"])
{
return NSPasteboardWritingPromised;
}
return nil;
}
and to do the pasting:
#pragma mark --- Paste functionality
+(NSArray *)readableTypesForPasteboard:(NSPasteboard *)pasteboard
{
return #[#"my.company.person"];
}
+ (NSPasteboardReadingOptions)readingOptionsForType:(NSString *)type pasteboard:(NSPasteboard *)pasteboard
{
if ( [type isEqualToString:#"my.company.person"])
{
return NSPasteboardReadingAsString;
}
return nil;
}
- (id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type
{
if ( [type isEqualToString:#"my.company.person"])
{
...
}
return nil;
}
How should I proceed here? I am at a loss, reading many stackoverflow Q&A's (e.g. Peter Hosey's great answer to NSPasteboard and simple custom data), as well as the Apple docs, still have me stumped on this one.
Have you already seen this:
Using Managed Objects?
I'm not sure what you mean about 'I need to return a Person object but I don't have a managed object context.' Your context is your scratch pad to create things.

How to transfer an NSObject pointer to selector in Object C?

I want to transfer an Object pointer as a parameter to me selector, however, it will not allow me to do that, is there anyway to do that? or is there any workaround here?
I have two Classes, A Class will do something and then the result feed back to B class.
what I am doing now in A instance:
MyObject *b;
[b addTarget:self withSelector:#selector(dosomething:) ];
-(void)dosomething:(NSString **)string
{
*string="some thing is implemented";
}
B instance:
{
NSString *mystring;
[a performSelector:dosomething withObject:&mystring];
}
Thanks.
You can only pass a valid Objective-C object to -performSelector:withObject:.
You have three options.
Pass a mutable string:
- (void)doSomething:(NSMutableString*)foo
{
[foo setString:#"Hello"];
}
Return a string:
- (NSString*)doSomething
{
return #"Hello";
}
Pass an NSValue that contains a NSString**:
- (void)doSomething:(NSValue*)value
{
NSString** stringPointer = (NSString**)[value pointerValue];
*stringPointer = #"Hello";
}

displaying a to-many relationship in a view (NSTableColumn) via Binding and Value Transformer

I have an entity Tag (in core data) that have some attributes and a to-many relationship named "aliases".
My ArrayController is "bind" in Xcode to:
Parameters->Managed Object Context = File's Owner.managedObjectContext
It work OK for all Attributes, the others columns present the correct values.
In one column I try to "display" this to-many relationship. If I do it naively and bind the Value of my NSTableColumn to my ArrayController.arrangedObjects.aliases (like all other attributes) I get this on the screen:
Relationship fault for (),
name aliases, isOptional 1, isTransient 0, entity Tag,
renamingIdentifier aliases, validation predicates ( ), warnings ( ),
versionHashModifier (null), destination entity TagAlias,
inverseRelationship tag, minCount 0, maxCount 0 on 0x10053db10
It seems to be some kind of CoreData proxy for the relationship...
I then build a subclass of NSValueTransformer:
#interface tagAliasesToStringTransformer : NSValueTransformer
+ (Class)transformedValueClass;
+ (BOOL)allowsReverseTransformation;
- (id)transformedValue:(id)value;
#end
and tried to use it as a Value Transformer in the binding. But I'm lost in my implementation:
#implementation tagAliasesToStringTransformer
+ (Class)transformedValueClass {
return [NSString class];
}
+ (BOOL)allowsReverseTransformation {
return NO;
}
- (id)transformedValue:(id)value {
if (value == nil) return nil;
...
}
#end
In the method transformedValue:, value is of class '_NSFaultingMutableSet' and I don't know how to get a Set/Array of the aliases or anything useful.
The goal is to build a NSString of the concatenation of each alias. Something like:
aTag : alias1 alias2 alias3 ...
I found the solution:
_NSFaultingMutableSet is actually a kind of NSSet, so by doing something like this:
- (id)transformedValue:(id)value {
if (value == nil) return nil;
NSArray *tags = [value allObjects];
for (Tag *tag in tags) {
...
}
}
you get access to all the entity.
Don't know why it's obvious now and not one week ago... getting out of newbie zone ?

Resources