Skip to content

Commit

Permalink
Implemented proper pointer handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-uc-service-account committed Sep 9, 2016
1 parent 0ed46ee commit e1ab5f7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 11 additions & 2 deletions ParseZero/PFObject+Additions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,17 @@ extension PFObject {
var value:AnyObject? = kv.1

// parse pointer
if key.containsString("_p_") {
var isPointer = false
var unprefixedKey: String = key
let prefix = key.substringToIndex(key.startIndex.advancedBy(3))
if prefix == "_p_" {
if let pointer = value as? String {
isPointer = true
let pointerStringComponents = pointer.componentsSeparatedByString("$")
let pointerClassName = pointerStringComponents[0]
let pointerObjectId = pointerStringComponents[1]
value = PFObject(withoutDataWithClassName: pointerClassName, objectId: pointerObjectId)
unprefixedKey = key.substringFromIndex(key.startIndex.advancedBy(3))
}
}

Expand Down Expand Up @@ -117,7 +122,11 @@ extension PFObject {
}

if let value = value {
self[kv.0] = value
if isPointer {
self[unprefixedKey] = value
} else {
self[kv.0] = value
}
}
}
self.cleanupOperationQueue()
Expand Down
12 changes: 11 additions & 1 deletion ParseZeroTests/ParseZeroObjC.m
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,18 @@ - (void)checkIntegrity
objects = [[[[[A1 relationForKey:@"bs"] query] fromLocalDatastore] ignoreACLs] findObjects];
XCTAssertEqual([objects count], 3);
[self ensureOperationSetQueueIsEmpty:objs];

// Check if we can query by pointer
PFObject *pObj = [PFObject objectWithoutDataWithClassName:@"ClassB" objectId:@"4"];
PFQuery *query = [PFQuery queryWithClassName:@"ClassA"];
[query whereKey:@"learningPath" equalTo:pObj];
NSArray *arr = [[[[query copy] fromPinWithName:PFObjectDefaultPin] ignoreACLs] findObjects];
XCTAssertEqual([arr count], 1);
PFObject *obj = arr[0];
XCTAssert([obj.objectId isEqualToString:@"1"]);

// Fetch A1 to test if the Pointer is properly set
NSString *bName = [A1[@"_p_learningPath"] fetchFromLocalDatastore][@"name"];
NSString *bName = [A1[@"learningPath"] fetchFromLocalDatastore][@"name"];
XCTAssert([bName isEqualToString:@"Object4"], @"bname should be set to Object4 instead %@", bName);
[self ensureOperationSetQueueIsEmpty:@[A1]];
}
Expand Down

0 comments on commit e1ab5f7

Please sign in to comment.