Skip to content

Commit

Permalink
added backup data in case server is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver K. Ernst committed Nov 29, 2020
1 parent b1a1837 commit d6511b4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
4 changes: 4 additions & 0 deletions TheMaskedManual.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
4B79C5F725745E5900AA16D7 /* data_2020_11_29.txt in Resources */ = {isa = PBXBuildFile; fileRef = 4B79C5F625745E5900AA16D7 /* data_2020_11_29.txt */; };
4B7A119E2571A59A00F22258 /* CompanySearchName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B7A119D2571A59A00F22258 /* CompanySearchName.swift */; };
4B7A11A12571AA5100F22258 /* CompanyTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4B7A11A02571AA5100F22258 /* CompanyTableViewCell.xib */; };
4B7A11A52571C37D00F22258 /* TrivialWordsRemover.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B7A11A42571C37D00F22258 /* TrivialWordsRemover.swift */; };
Expand Down Expand Up @@ -45,6 +46,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
4B79C5F625745E5900AA16D7 /* data_2020_11_29.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = data_2020_11_29.txt; sourceTree = "<group>"; };
4B7A119D2571A59A00F22258 /* CompanySearchName.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompanySearchName.swift; sourceTree = "<group>"; };
4B7A11A02571AA5100F22258 /* CompanyTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = CompanyTableViewCell.xib; sourceTree = "<group>"; };
4B7A11A42571C37D00F22258 /* TrivialWordsRemover.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrivialWordsRemover.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -191,6 +193,7 @@
4BC6B8AC256885830075D1AD /* AppDelegate.swift */,
4BC6B8AE256885830075D1AD /* SceneDelegate.swift */,
4B7EAB312570A46600C69D05 /* LoadMasks.swift */,
4B79C5F625745E5900AA16D7 /* data_2020_11_29.txt */,
4B7A11A32571C36600F22258 /* StringManipulations */,
4BB6FF01256F7FA800A72429 /* DataStructures */,
4B7EAB282570A3A500C69D05 /* Helpers */,
Expand Down Expand Up @@ -264,6 +267,7 @@
4BB1741B256D979D005EAC86 /* MaskNotFoundHeaderView.xib in Resources */,
4BC6B8B6256885850075D1AD /* Assets.xcassets in Resources */,
4B7A11A12571AA5100F22258 /* CompanyTableViewCell.xib in Resources */,
4B79C5F725745E5900AA16D7 /* data_2020_11_29.txt in Resources */,
4BC6B8B4256885830075D1AD /* Main.storyboard in Resources */,
4BDCEDAA256E301C0035B51F /* MaskTableViewCell.xib in Resources */,
4B7A11C3257200A400F22258 /* Launch Screen.storyboard in Resources */,
Expand Down
22 changes: 15 additions & 7 deletions TheMaskedManual/LoadMasks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,13 @@ struct LoadMasks {

static func load_masks_and_companies(completion: @escaping ([Mask], [Company]) -> Void) {

// Load current data
load_masks_and_companies_from_local(completion: completion)

// Also: check if data should be downloaded, and if so, download asynch
if check_if_data_should_be_downloaded() {
print("Downloading latest data...")
download_latest_data {
load_masks_and_companies_from_local(completion: completion)
}
} else {
print("Loading data from local storage...")
load_masks_and_companies_from_local(completion: completion)
download_latest_data(completion: {})
}
}

Expand Down Expand Up @@ -289,7 +288,16 @@ struct LoadMasks {

} catch {
print ("url error: \(error)")
completion(nil)

// Try the backup
if let path = Bundle.main.path(forResource: "data_2020_11_29", ofType: "txt") {
print("Switching to backup")
completion(URL(fileURLWithPath: path))
} else {
// Very bad!
print ("url error for backup!")
completion(nil)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions TheMaskedManual/data_2020_11_29.txt

Large diffs are not rendered by default.

0 comments on commit d6511b4

Please sign in to comment.