I have a class:
class Item: NSManagedObject {
func fetchProducts() {
let backgroundQueue = NSOperationQueue()
backgroundQueue.maxConcurrentOperationCount = 5
for n in items {
backgroundQueue.addOperationWithBlock(){
self.someFunc(n)
}
}
}
}
If i understand, for each instance i create max 5 threads. But i want to create global NSOperationQueue()
I created global struct:
struct GVariables {
static let globalBackgroundQueue = NSOperationQueue()
}
The problem: How to set maxConcurrentOperationCount
and other settings ?
Because if i modify method like this:
func fetchProducts() {
GVariables.globalBackgroundQueue.maxConcurrentOperationCount = 5
}
It will be the same ? or not ?
If I understand what you're after, I would do something more like this:
struct GlobalQueue {
static let globalBackgroundQueue = GlobalQueue()
let backgroundQueue = NSOperationQueue()
private init () {
backgroundQueue.maxConcurrentOperationCount = 5
//... more settings
}
}
This is a singleton that holds an instance of itself. You can initialize whatever values you want in your initializer. Note the private keyword which insures that you can't make more than one instance of this class.
Obligatory Disclaimer: beware singletons...