我想知道是否已经可以使用Realm和Swift 3.0的beta版.
虽然我在Realm repo中看到了RealmSwift-swift3.0目录,但我没有成功正确使用它.我应该如何从源代码安装它?
为了在日志文件中存储类名,我将类类型的描述转换为字符串:
let objectType: NSObject.Type = Object.self
let str = String(describing: objectType)
Run Code Online (Sandbox Code Playgroud)
但是,我没有成功地将其转换str
为类型变量NSObject.Type
以在泛型方法中使用它.
我怎么能这样做?
我有一组异步执行的请求。但是,每个下一个请求应该只在前一个请求完成时开始(由于数据依赖性)。
由于所有请求都应该以正确的顺序完成,因此DispatchGroup()
似乎没有用。
我目前实现了DispatchSemaphore()
,但我觉得这不是最好的解决方案,因为我想确保所有请求都在后台执行。
let semaphore = DispatchSemaphore(value: requests.count)
for request in requests {
apiManager().performAsyncRequest(request, failure: { error in
print(error); semaphore.signal()
}) { print(“request finished successful”)
// Next request should be performed now
semaphore.signal()
}
}
semaphore.wait()
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来执行此操作?
注意:基于我遇到的以下答案之一的实现,该答案apiManager()
不是线程安全的(由于使用了 Realm 数据库)。
为了使这个问题清晰,并以线程安全的方式考虑答案,线程安全定义为performAsyncRequest
:
public func performAsyncRequest(_ requestNumber: Int, success: @escaping (Int) -> Void)->Void {
DispatchQueue(label: "performRequest").async {
usleep(useconds_t(1000-requestNumber*200))
print("Request #\(requestNumber) starts")
success(requestNumber)
}
}
Run Code Online (Sandbox Code Playgroud)
解决方案 DispatchSemaphore
let semaphore = DispatchSemaphore(value: 1)
DispatchQueue(label: "requests").async …
Run Code Online (Sandbox Code Playgroud) multithreading asynchronous grand-central-dispatch ios swift
当执行彼此依赖的操作时,OperationQueue
可以使用它们来确保它们以正确的顺序执行.但是,是否也可以确保操作一个接一个地完成?
让我们假设一个异步执行的方法,需要一些时间才能完成:
public func performOperation(_ number: Int, success: @escaping (Int) -> Void)->Void {
DispatchQueue(label: "operations").async {
print("Operation #\(number) starts")
usleep(useconds_t(1000-number*200)) // Block thread for some time
success(number)
}
}
Run Code Online (Sandbox Code Playgroud)
操作和依赖关系创建如下:
let operationQueue = OperationQueue.main
for operationNumber in 0..<4 { // Create operations as an example
let operation = BlockOperation(block: {
performOperation(operationNumber) { number in
DispatchQueue.main.sync {
print("Operation #\(number) finished")
}
}
})
operation.name = "Operation #\(operationNumber)"
if operationNumber > 0 {
operation.addDependency(operationQueue.operations.last!)
// Print dependencies
print("\(operation.name!) should finish …
Run Code Online (Sandbox Code Playgroud) 我想执行多个 Alamofire 请求。但是,由于数据依赖性,新请求应仅在前一个请求完成时开始。
我已经用更一般的异步请求示例提出了一个问题,该示例使用OperationQueue
. 但是,我没有成功地使用 Alamofire 实现相同的目标。
public func performAlamofireRequest(_ number: Int, success: @escaping (Int) -> Void)->Void {
Alamofire.request(String(format: "http://jsonplaceholder.typicode.com/posts/%i", number+1)) // NSURLSession dispatch queue
.responseString { response in // Completion handler at main dispatch queue?
if response.result.isSuccess {
// print("data")
} else if response.result.isFailure {
// print("error")
}
success(number) // Always leave closure in this example
}
}
Run Code Online (Sandbox Code Playgroud)
为了确保在下一个请求开始之前完成请求,我使用OperationQueue
如下:
let operationQueue = OperationQueue.main
for operationNumber in 0..<4 { // Create some operations
let …
Run Code Online (Sandbox Code Playgroud) swift ×5
ios ×4
alamofire ×1
asynchronous ×1
concurrency ×1
generics ×1
metaclass ×1
nsoperation ×1
realm ×1
swift3 ×1