Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ final class StockSymbolStorgeService {
)!
)

func persist(stockSymbols: [StockSymbol]) {
func persist(
stockSymbols: [StockSymbol],
completion: @escaping () -> Void
) {
stockSymbols.forEach { symbol in
storageService.persist(
updateWith: { (dbSymbol: DBStockSymbol) in
dbSymbol.updateWithDomainModel(symbol)
},
predicate: NSPredicate(format: "symbol = %@", "\(symbol.symbol)")
predicate: NSPredicate(format: "symbol = %@", "\(symbol.symbol)"),
completion: completion
)
}
}
Expand Down
7 changes: 4 additions & 3 deletions DevPods/StockList/StockList/StockListPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ extension StockListPresenter {
) {
switch $0 {
case .success(let stocks):
self.storageService.persist(stockSymbols: stocks)
self.stocks = stocks
self.view?.showTable(with: stocks.map(StockDisplayModel.init))
self.storageService.persist(stockSymbols: stocks) {
self.stocks = stocks
self.view?.showTable(with: stocks.map(StockDisplayModel.init))
}
case .failure:
self.view?.showError()
}
Expand Down
4 changes: 2 additions & 2 deletions DevPods/Storage/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PODFILE CHECKSUM: 5259f5868453460955f33295d8d76460bade8cee
PODFILE CHECKSUM: 28a690778519c7cfb0688c8b2f0df675c0cf0abe

COCOAPODS: 1.8.4
COCOAPODS: 1.9.0
22 changes: 13 additions & 9 deletions DevPods/Storage/Storage.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@
objects = {

/* Begin PBXBuildFile section */
3D7FC6FA6B39EFA82189AE56 /* Pods_Storage.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 96E5B5075D9B764A83369F32 /* Pods_Storage.framework */; };
D9BE6F94240BFA8A005D9A3D /* StorageService.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9BE6F93240BFA8A005D9A3D /* StorageService.swift */; };
D9BE6F96240BFB28005D9A3D /* DBInsertable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9BE6F95240BFB28005D9A3D /* DBInsertable.swift */; };
EA64D4B3A220B7CF00125FFF /* Pods_Container.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E9C71DF0779063A1FE3F1F38 /* Pods_Container.framework */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
21647414F3C4263CA586C215 /* Pods-Container.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Container.debug.xcconfig"; path = "Target Support Files/Pods-Container/Pods-Container.debug.xcconfig"; sourceTree = "<group>"; };
4405B5D406F7C81598FDCA90 /* Pods-Storage.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Storage.debug.xcconfig"; path = "Target Support Files/Pods-Storage/Pods-Storage.debug.xcconfig"; sourceTree = "<group>"; };
86F52DFEDC248438C25D6756 /* Pods-Container.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Container.release.xcconfig"; path = "Target Support Files/Pods-Container/Pods-Container.release.xcconfig"; sourceTree = "<group>"; };
96E5B5075D9B764A83369F32 /* Pods_Storage.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Storage.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D9BE6F88240BED33005D9A3D /* Storage.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Storage.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D9BE6F8C240BED33005D9A3D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D9BE6F93240BFA8A005D9A3D /* StorageService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageService.swift; sourceTree = "<group>"; };
D9BE6F95240BFB28005D9A3D /* DBInsertable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DBInsertable.swift; sourceTree = "<group>"; };
E9C71DF0779063A1FE3F1F38 /* Pods_Container.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Container.framework; sourceTree = BUILT_PRODUCTS_DIR; };
E63F0C955B35A82BDF62ECC4 /* Pods-Storage.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Storage.release.xcconfig"; path = "Target Support Files/Pods-Storage/Pods-Storage.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
D9BE6F85240BED33005D9A3D /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
EA64D4B3A220B7CF00125FFF /* Pods_Container.framework in Frameworks */,
3D7FC6FA6B39EFA82189AE56 /* Pods_Storage.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -37,7 +39,7 @@
1B77DDF426405A68DA4E8AA3 /* Frameworks */ = {
isa = PBXGroup;
children = (
E9C71DF0779063A1FE3F1F38 /* Pods_Container.framework */,
96E5B5075D9B764A83369F32 /* Pods_Storage.framework */,
);
name = Frameworks;
sourceTree = "<group>";
Expand All @@ -47,6 +49,8 @@
children = (
21647414F3C4263CA586C215 /* Pods-Container.debug.xcconfig */,
86F52DFEDC248438C25D6756 /* Pods-Container.release.xcconfig */,
4405B5D406F7C81598FDCA90 /* Pods-Storage.debug.xcconfig */,
E63F0C955B35A82BDF62ECC4 /* Pods-Storage.release.xcconfig */,
);
path = Pods;
sourceTree = "<group>";
Expand Down Expand Up @@ -170,7 +174,7 @@
outputFileListPaths = (
);
outputPaths = (
"$(DERIVED_FILE_DIR)/Pods-Container-checkManifestLockResult.txt",
"$(DERIVED_FILE_DIR)/Pods-Storage-checkManifestLockResult.txt",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand Down Expand Up @@ -314,15 +318,15 @@
};
D9BE6F91240BED33005D9A3D /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 21647414F3C4263CA586C215 /* Pods-Container.debug.xcconfig */;
baseConfigurationReference = 4405B5D406F7C81598FDCA90 /* Pods-Storage.debug.xcconfig */;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = Container/Info.plist;
INFOPLIST_FILE = Storage/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand All @@ -340,15 +344,15 @@
};
D9BE6F92240BED33005D9A3D /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 86F52DFEDC248438C25D6756 /* Pods-Container.release.xcconfig */;
baseConfigurationReference = E63F0C955B35A82BDF62ECC4 /* Pods-Storage.release.xcconfig */;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
INFOPLIST_FILE = Container/Info.plist;
INFOPLIST_FILE = Storage/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down
10 changes: 10 additions & 0 deletions DevPods/Storage/Storage.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

такс

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
25 changes: 15 additions & 10 deletions DevPods/Storage/Storage/StorageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public final class StorageService {
return container
}()

public var viewContext: NSManagedObjectContext {
private var viewContext: NSManagedObjectContext {
container.viewContext
}
private lazy var backgroundContext = container.newBackgroundContext()

public func fetch<T: DBInsertable>(predicate: NSPredicate?) -> [T]? {
var objects: [T]?
Expand All @@ -48,20 +49,24 @@ public final class StorageService {
}

public func persist<T: DBInsertable>(
updateWith: (T) -> Void,
predicate: NSPredicate
updateWith: @escaping (T) -> Void,
predicate: NSPredicate,
completion: @escaping () -> Void
) {
viewContext.performAndWait {
backgroundContext.perform {
T.insertOrUpdate(
context: viewContext,
context: self.viewContext,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это выглядит не хорошо - ты делаешь операцию на одном контексте а вставляешь на другом

updateWith: updateWith,
predicate: predicate
)
}
do {
try viewContext.save()
} catch {
viewContext.rollback()
do {
try self.viewContext.save()
} catch {
self.viewContext.rollback()
}
DispatchQueue.main.async {
completion()
}
}
}
}