remove weak self capturing and deallocated error

This commit is contained in:
Ivan Smolin 2016-11-23 17:31:16 +03:00
parent 4632d37507
commit e69531c9ae
4 changed files with 18 additions and 42 deletions

View File

@ -38,38 +38,26 @@ public class FixedPageCursor<Cursor: CursorType>: CursorType where Cursor.LoadRe
}
public func loadNextBatch() -> Observable<LoadResultType> {
return Observable.create { [weak self] observer in
guard let strongSelf = self else {
observer.onError(CursorError.deallocated)
return Disposables.create()
}
if strongSelf.exhausted {
return Observable.create { observer in
if self.exhausted {
observer.onError(CursorError.exhausted)
return Disposables.create()
}
let restOfLoaded = strongSelf.cursor.count - strongSelf.count
let restOfLoaded = self.cursor.count - self.count
if restOfLoaded >= strongSelf.pageSize || strongSelf.cursor.exhausted {
let startIndex = strongSelf.count
strongSelf.count += min(restOfLoaded, strongSelf.pageSize)
if restOfLoaded >= self.pageSize || self.cursor.exhausted {
let startIndex = self.count
self.count += min(restOfLoaded, self.pageSize)
observer.onNext(startIndex..<strongSelf.count)
observer.onNext(startIndex..<self.count)
return Disposables.create()
}
return strongSelf.cursor.loadNextBatch()
.map { [weak self] _ -> Observable<LoadResultType> in
guard let strongSelf = self else {
throw CursorError.deallocated
}
return strongSelf.loadNextBatch()
}
return self.cursor.loadNextBatch()
.map { _ in self.loadNextBatch() }
.subscribe()
}
}

View File

@ -58,15 +58,11 @@ public class MapCursor<Cursor: CursorType, T>: CursorType where Cursor.LoadResul
}
public func loadNextBatch() -> Observable<LoadResultType> {
return cursor.loadNextBatch().map { [weak self] loadedRange -> LoadResultType in
guard let strongSelf = self else {
throw CursorError.deallocated
}
return cursor.loadNextBatch().map { loadedRange in
let startIndex = self.elements.count
self.elements += self.cursor[loadedRange].flatMap(self.transform)
let startIndex = strongSelf.elements.count
strongSelf.elements += strongSelf.cursor[loadedRange].flatMap(strongSelf.transform)
return startIndex..<strongSelf.elements.count
return startIndex..<self.elements.count
}
}

View File

@ -31,24 +31,18 @@ public class StaticCursor<Element>: CursorType {
}
public func loadNextBatch() -> Observable<LoadResultType> {
return Observable.create { [weak self] observer in
guard let strongSelf = self else {
observer.onError(CursorError.deallocated)
return Disposables.create()
}
if strongSelf.exhausted {
return Observable.create { observer in
if self.exhausted {
observer.onError(CursorError.exhausted)
return Disposables.create()
}
strongSelf.count = strongSelf.content.count
self.count = self.content.count
strongSelf.exhausted = true
self.exhausted = true
observer.onNext(0..<strongSelf.count)
observer.onNext(0..<self.count)
return Disposables.create()
}

View File

@ -12,11 +12,9 @@ import Foundation
///
/// - busy: cursor is currently processing another request
/// - exhausted: cursor did load all available results
/// - deallocated: cursor was deallocated during processing request
public enum CursorError: Error {
case busy
case exhausted
case deallocated
}