문서 메뉴
문서 홈
/ /
Atlas Device SDK
/ /

함수 호출 - Swift SDK

이 페이지의 내용

  • 이름으로 함수 호출
  • 비동기/대기 함수 호출

중요

Functions를 사용할 때 코드 삽입을 방지하기 위해 클라이언트 데이터를 삭제해야 합니다.

Consider an Atlas App Services Function named concatenate that takes two arguments, concatenates them, and returns the result:

// concatenate: concatenate two strings
exports = function(a, b) {
return a + b;
};

Swift SDK에서 함수를 실행하려면 현재 로그인한 사용자에서 functions 객체를 사용합니다.

functions 객체에는 함수에 해당하는 동적 멤버가 있습니다. 이 경우 functions.concatenate() 은(는) concatenate 함수를 나타냅니다. BSONArray 의 인수를 전달합니다. 후행 클로저는 함수 호출이 완료될 때 호출하는 완료 핸들러입니다. 이 핸들러는 메인이 아닌 글로벌 DispatchQueue 에서 실행됩니다.

RLMApp *app = [RLMApp appWithId:YOUR_APP_ID];
// ... log in ...
RLMUser *user = [app currentUser];
// Call concatenate function
[user callFunctionNamed:@"concatenate"
arguments:@[@"john.smith", @"@companyemail.com"]
completionBlock:^(id<RLMBSON> result, NSError *error) {
if (error) {
NSLog(@"Function call failed: %@", [error localizedDescription]);
return;
}
NSLog(@"Called function 'concatenate' and got result: %@", result);
assert([result isEqual:@"john.smith@companyemail.com"]);
}];
let app = App(id: YOUR_APP_SERVICES_APP_ID)
// ... log in ...
let user = app.currentUser!
// The dynamic member name `concatenate` is directly associated with the
// function name. The first argument is the `BSONArray` of arguments to be
// provided to the function - in this case, a string that represents a
// username and a string that represents an email domain.
// The trailing closure is the completion handler to call when the function
// call is complete. This handler is executed on a non-main global
// `DispatchQueue`.
user.functions.concatenate([AnyBSON("john.smith"), AnyBSON("@companyemail.com")]) { concatenate, error in
guard error == nil else {
print("Function call failed: \(error!.localizedDescription)")
return
}
guard case let .string(value) = concatenate else {
print("Unexpected non-string result: \(concatenate ?? "nil")")
return
}
print("Called function 'concatenate' and got result: \(value)")
assert(value == "john.smith@companyemail.com")
}

버전 10.16.0의 새로운 기능.

Realm Swift SDK는 User.function 메서드의 비동기/대기 버전을 제공합니다.

func testAsyncCallFunction() async {
let app = App(id: YOUR_APP_SERVICES_APP_ID)
// ... log in ...
let user = app.currentUser!
do {
// The dynamic member name `concatenate` is directly associated with the
// function name. The first argument is the `BSONArray` of arguments to be
// provided to the function - in this case, a string that represents a
// username and a string that represents an email domain.
let concatenatedString = try await user.functions.concatenate([AnyBSON("john.smith"), AnyBSON("@companyemail.com")])
print("Called function 'concatenate' and got result: \(concatenatedString)")
assert(concatenatedString == "john.smith@companyemail.com")
} catch {
print("Function call failed: \(error.localizedDescription)")
}
}

Realm Swift SDK 버전 10.15.0 및 10.16.0부터 많은 Realm API가 Swift async/await 구문을 지원합니다. 프로젝트는 다음 요구 사항을 충족해야 합니다.

Swift SDK 버전
Swift 버전 요구 사항
지원되는 OS
10.25.0
Swift 5.6
iOS 13.x
10.15.0 또는 10.16.0
Swift 5.5
iOS 15.x

앱이 async/await 컨텍스트에서 Realm에 액세스하는 경우 코드를 @MainActor(으)로 표시하여 스레드 관련 충돌을 방지합니다.

돌아가기

App Services 앱에 연결하

다음

MongoDB 쿼리