diff --git a/AVOS/LeanCloudObjcTests/BaseTestCase.swift b/AVOS/LeanCloudObjcTests/BaseTestCase.swift index 3424bcb7..8470c4e5 100644 --- a/AVOS/LeanCloudObjcTests/BaseTestCase.swift +++ b/AVOS/LeanCloudObjcTests/BaseTestCase.swift @@ -69,6 +69,11 @@ class BaseTestCase: XCTestCase { super.tearDown() } + override func setUp() { + super.setUp() + LCUser.logOut() + } + override func tearDown() { LCUser.logOut() super.tearDown() diff --git a/AVOS/LeanCloudObjcTests/LCUserTestCase.swift b/AVOS/LeanCloudObjcTests/LCUserTestCase.swift index 32330792..cfa60cfe 100644 --- a/AVOS/LeanCloudObjcTests/LCUserTestCase.swift +++ b/AVOS/LeanCloudObjcTests/LCUserTestCase.swift @@ -330,4 +330,46 @@ class LCUserTestCase: BaseTestCase { } } } + + func testStrictlyFind() { + expecting { exp in + LCUser.loginAnonymously { user, error in + XCTAssertNotNil(user) + XCTAssertNil(error) + exp.fulfill() + } + } + guard let user = LCUser.current() else { + XCTFail() + return + } + let hiddenField = "hiddenField" + let exposedField = "exposedField" + user[hiddenField] = uuid + user[exposedField] = uuid + XCTAssertTrue(user.save()) + + expecting { exp in + let query = LCQuery() + query.whereKey(hiddenField, equalTo: user[hiddenField] ?? "") + LCUser.strictlyFind(with: query) { users, error in + XCTAssertTrue(Thread.isMainThread) + XCTAssertNil(users) + XCTAssertNotNil(error) + exp.fulfill() + } + } + + expecting { exp in + let query = LCQuery() + query.whereKey(exposedField, equalTo: user[exposedField] ?? "") + LCUser.strictlyFind(with: query) { users, error in + XCTAssertTrue(Thread.isMainThread) + XCTAssertEqual(users?.count, 1) + XCTAssertEqual(users?.first?.objectId, user.objectId) + XCTAssertNil(error) + exp.fulfill() + } + } + } } diff --git a/AVOS/Sources/Foundation/Query/LCQuery.m b/AVOS/Sources/Foundation/Query/LCQuery.m index be7c5705..733ab30e 100644 --- a/AVOS/Sources/Foundation/Query/LCQuery.m +++ b/AVOS/Sources/Foundation/Query/LCQuery.m @@ -1012,39 +1012,30 @@ + (NSDictionary *)dictionaryFromIncludeKeys:(NSArray *)array { - (NSMutableDictionary *)assembleParameters { [self.parameters removeAllObjects]; - - if ([self.where allKeys].count > 0) - { + if (self.where.count > 0) { [self.parameters setObject:[self whereString] forKey:@"where"]; } - - if (self.limit > 0) - { + if (self.limit > 0) { [self.parameters setObject:@(self.limit) forKey:@"limit"]; } - if (self.skip > 0) - { + if (self.skip > 0) { [self.parameters setObject:@(self.skip) forKey:@"skip"]; } - if (self.order.length > 0) - { + if (self.order.length > 0) { [self.parameters setObject:self.order forKey:@"order"]; } - if (self.include.count > 0) - { - NSString * myIncludes = [[self.include allObjects] componentsJoinedByString:@","]; - [self.parameters setObject:myIncludes forKey:@"include"]; + if (self.include.count > 0) { + NSString *includes = [[self.include allObjects] componentsJoinedByString:@","]; + [self.parameters setObject:includes forKey:@"include"]; } - if (self.selectedKeys.count > 0) - { - NSString * keys = [[self.selectedKeys allObjects] componentsJoinedByString:@","]; + if (self.selectedKeys.count > 0) { + NSString *keys = [[self.selectedKeys allObjects] componentsJoinedByString:@","]; [self.parameters setObject:keys forKey:@"keys"]; } - if (self.includeACL) - { + if (self.includeACL) { [self.parameters setObject:@"true" forKey:@"returnACL"]; } - if ([self.extraParameters allKeys].count > 0) { + if (self.extraParameters.count > 0) { [self.parameters addEntriesFromDictionary:self.extraParameters]; } return self.parameters; diff --git a/AVOS/Sources/Foundation/Query/LCQuery_Internal.h b/AVOS/Sources/Foundation/Query/LCQuery_Internal.h index e08ec882..c4bf3cd1 100644 --- a/AVOS/Sources/Foundation/Query/LCQuery_Internal.h +++ b/AVOS/Sources/Foundation/Query/LCQuery_Internal.h @@ -14,7 +14,7 @@ @property (nonatomic) NSMutableSet *selectedKeys; @property (nonatomic, strong) NSMutableDictionary *extraParameters; -- (NSDictionary *)assembleParameters; +- (NSMutableDictionary *)assembleParameters; + (NSDictionary *)dictionaryFromIncludeKeys:(NSArray *)array; - (NSString *)queryPath; -(void)queryWithBlock:(NSString *)path diff --git a/AVOS/Sources/Foundation/User/LCUser.h b/AVOS/Sources/Foundation/User/LCUser.h index 5b7be566..800cbd9d 100644 --- a/AVOS/Sources/Foundation/User/LCUser.h +++ b/AVOS/Sources/Foundation/User/LCUser.h @@ -500,6 +500,15 @@ FOUNDATION_EXPORT LeanCloudSocialPlatform const LeanCloudSocialPlatformWeiXin; */ - (BOOL)isAnonymous; +// MARK: Strictly Find + +/// More restrictive on query conditions to find user. +/// Constraints: NOT support `skip`; NOT support the protected fields; NOT support `inQuery` ... +/// @param query The query conditions. +/// @param callback Result callback. ++ (void)strictlyFindWithQuery:(LCQuery *)query + callback:(void (^)(NSArray * _Nullable users, NSError * _Nullable error))callback; + @end /** diff --git a/AVOS/Sources/Foundation/User/LCUser.m b/AVOS/Sources/Foundation/User/LCUser.m index 85a4d34c..f3c11ed2 100644 --- a/AVOS/Sources/Foundation/User/LCUser.m +++ b/AVOS/Sources/Foundation/User/LCUser.m @@ -6,7 +6,7 @@ #import "LCObject_Internal.h" #import "LCPaasClient.h" #import "LCUtils_Internal.h" -#import "LCQuery.h" +#import "LCQuery_Internal.h" #import "LCPersistenceUtils.h" #import "LCObjectUtils.h" #import "LCPaasClient.h" @@ -1191,6 +1191,30 @@ - (BOOL)isAnonymous return [[self linkedServiceNames] containsObject:anonymousTag]; } +// MARK: Strictly Find + ++ (void)strictlyFindWithQuery:(LCQuery *)query + callback:(void (^)(NSArray * _Nullable, NSError * _Nullable))callback +{ + [[LCPaasClient sharedInstance] getObject:@"users/strictlyQuery" + withParameters:[query assembleParameters] + block:^(id _Nullable object, NSError * _Nullable error) { + NSMutableArray *users; + if (!error && [NSDictionary _lc_isTypeOf:object]) { + NSArray *results = [NSArray _lc_decoding:object key:@"results"]; + users = [NSMutableArray arrayWithCapacity:results.count]; + for (NSDictionary *dictionary in results) { + if ([NSDictionary _lc_isTypeOf:dictionary]) { + LCUser *user = (LCUser *)[LCObjectUtils lcObjectForClass:[LCUser userTag]]; + [LCObjectUtils copyDictionary:dictionary toObject:user]; + [users addObject:user]; + } + } + } + [LCUtils callArrayResultBlock:callback array:users error:error]; + }]; +} + #pragma mark - Override from LCObject /**