Start a new topic

Remove object from dataBase with KCSQuery

Guys, I'm trying to remove an object from my database using a query. The problem is nothing is removed.



Is my code right?



KCSQuery *q = [KCSQuery query];



[q addQueryOnField:@"user._id" withExactMatchForValue:self.userId];

[q addQueryOnField:@"loved._id" withExactMatchForValue:lovedOneID];



NSLog(@"%@", [q JSONStringRepresentation]);



[[Loved appDataStore] removeObject:q withCompletionBlock:^(NSArray *objectsOrNil, NSError *errorOrNil) {



NSLog(@"removed: %@, %@", objectsOrNil, errorOrNil);



completeBlock( objectsOrNil, errorOrNil );



} withProgressBlock:^(NSArray *objects, double percentComplete) {



}];



Thank you

I tried to query before deleting by doing this:



KCSQuery *q = [KCSQuery query];



[q addQueryOnField:@"user._id" withExactMatchForValue:self.userId];

[q addQueryOnField:@"loved._id" withExactMatchForValue:lovedOneID];



[[Loved appDataStore] queryWithQuery:q withCompletionBlock:^(NSArray *objectsOrNil, NSError *errorOrNil) {



if( !errorOrNil && [objectsOrNil count] )

{

Loved *object = [objectsOrNil firstObject];

[object deleteFromCollection:[object appDataCollection] withCompletionBlock:^(NSArray *objectsOrNil, NSError *errorOrNil) {

NSLog(@"remove loved one: %@, %@", objectsOrNil, errorOrNil);

completeBlock( objectsOrNil, errorOrNil );

} withProgressBlock:^(NSArray *objects, double percentComplete) {



}];

}

else

{

NSLog(@"%@ %@", objectsOrNil, errorOrNil);

completeBlock( objectsOrNil, errorOrNil );

}



} withProgressBlock:^(NSArray *objects, double percentComplete) {



}];



And got this error:



Error Domain=KCSAppDataErrorDomain Code=401 "Entity operation was unsuccessful." UserInfo=0x153aa770 {NSLocalizedDescription=Entity operation was unsuccessful., kinveyRequestId=0989fbb4fa944701afdb294b96ad96a4, kinveyInternalErrorString=, NSLocalizedFailureReason=, NSErrorFailingURLStringKey=https://baas.kinvey.com/appdata/kid_TeQ3pmb__f/Loved/528227227198081e7d000022, kinveyErrorCode=InsufficientCredentials, NSLocalizedRecoverySuggestion=Retry request based on information in `NSLocalizedFailureReasonErrorKey`}
Michael, I did that. Querying with the same KCSQuery object, returns one object from server.



I'm verifying by looking into the database and also the NSLog says "removed: 0"
Maybe. This will remove any objects where it's user._id field is the same as self.userId, and loved._id is the same as lovedOneID.



You may want to try querying first just to see if there any objects that match the query. Also is there an error? How are you verifying that nothing is removed?
Login or Signup to post a comment