IOS_Boss/IOS_study/Extension/LeanCloud+.swift
2025-07-27 12:33:06 +08:00

183 lines
5.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// LeanCloud+.swift
// leancloud
//
// Created by CC-star on 2025/7/2.
//
import Foundation
import LeanCloud
extension LCQuery {
func getFirst() async throws -> LCObject {
try await withCheckedThrowingContinuation { continuation in
_ = self.getFirst { result in
switch result {
case .success(object: let lcObject):
continuation.resume(returning: lcObject)
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
///
func count() async throws -> Int {
try await withCheckedThrowingContinuation { continuation in
self.count { result in
switch result {
case .success(let count):
continuation.resume(returning: count)
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
//lcobject
func find() async throws -> [LCObject] {
try await withCheckedThrowingContinuation { continuation in
self.find { result in
switch result {
case .success(let objects):
continuation.resume(returning: objects)
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
//
func find<T: Decodable>(as type: T.Type) async throws -> [T] {
let lcObjects = try await withCheckedThrowingContinuation { continuation in
self.find { result in
switch result {
case .success(objects: let lcObjects):
continuation.resume(returning: lcObjects)
case .failure(error: let error):
continuation.resume(throwing: error)
}
}
}
var res: [T] = []
for lcObject in lcObjects {
//dictionaryValue
guard let lcDic = lcObject.dictionaryValue else { throw UserError.lcObjToDicError }
let data = try JSONSerialization.data(withJSONObject: lcDic)
let object = try JSONDecoder().decode(T.self, from: data)
res.append(object)
}
return res
}
}
extension LCObject {
// LeanCloud async
func save() async throws {
try await withCheckedThrowingContinuation { continuation in
_ = self.save() { result in
switch result {
case .success:
//
continuation.resume()
case .failure(let error):
//
continuation.resume(throwing: error)
}
}
}
}
///
func delete() async throws {
try await withCheckedThrowingContinuation { continuation in
_ = self.delete { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
///
static func delete(_ objects: [LCObject]) async throws {
try await withCheckedThrowingContinuation { continuation in
self.delete(objects) { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
}
extension LCSMSClient {
//requestShortMessage()async
static func requestShortMessage(
mobilePhoneNumber: String,
templateName: String,
signatureName: String
) async throws {
try await withCheckedThrowingContinuation { continuation in
LCSMSClient.requestShortMessage(
mobilePhoneNumber: mobilePhoneNumber,
templateName: templateName,
signatureName: signatureName
// ,variables: ["name": LCString("BOSS"), "ttl": LCNumber("5")]
) { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
}
extension LCUser {
//signUpOrLogIn()async
static func signUpOrLogIn(mobilePhoneNumber: String, verificationCode: String) async throws -> LCUser {
try await withCheckedThrowingContinuation { continuation in
_ = LCUser.signUpOrLogIn(mobilePhoneNumber: mobilePhoneNumber, verificationCode: verificationCode) { result in
switch result {
case .success(object: let user):
continuation.resume(returning: user)
case .failure(error: let error):
continuation.resume(throwing: error)
}
}
}
}
}
extension LCFile {
func save() async throws -> String {
try await withCheckedThrowingContinuation { continuation in
_ = self.save { result in
switch result {
case .success:
if let url = self.url?.value {
continuation.resume(returning: url) // URL
} else {
let error = UserError.saveFileSuccessButNoURL //
continuation.resume(throwing: error)
}
case .failure(let error):
continuation.resume(throwing: error) // LeanCloud
}
}
}
}
}