Skip to content
Merged
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
52 changes: 52 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--swiftversion 5.5

--exclude Pods

--rules anyObjectProtocol

--rules braces

--rules duplicateImports
--rules elseOnSameLine

--rules strongifiedSelf
--rules andOperator

--rules indent
--indent 4
--indentcase false
--indentstrings true
--ifdef outdent

--rules leadingDelimiters
--rules linebreakAtEndOfFile

--rules redundantGet
--rules redundantInit
--rules redundantParens
--rules redundantVoidReturnType
--rules semicolons
--rules spaceAroundBraces
--rules spaceAroundBrackets
--rules spaceAroundGenerics

--rules spaceAroundOperators
--rules spaceAroundParens
--rules spaceInsideBraces
--rules spaceInsideBrackets
--rules spaceInsideComments
--rules spaceInsideGenerics
--rules spaceInsideParens
--rules todos

--rules trailingCommas
--rules trailingSpace
--rules typeSugar
--rules void

--rules wrapArguments
--wraparguments before-first
--wrapcollections before-first
--wrapparameters before-first

--rules wrapMultilineStatementBraces
50 changes: 32 additions & 18 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,64 @@ disabled_rules:
- function_body_length
- line_length
opt_in_rules:
- sorted_first_last
- capture_variable
- closure_spacing
- closure_end_indentation
- collection_alignment
- comment_spacing
- conditional_returns_on_newline
- contains_over_first_not_nil
- contains_over_filter_count
- contains_over_filter_is_empty
- contains_over_range_nil_comparison
- discarded_notification_center_observer
- empty_count
- empty_collection_literal
- empty_string
- empty_xctest_method
- explicit_init
- fatal_error_message
- first_where
- flatmap_over_map_reduce
- identical_operands
- implicitly_unwrapped_optional
- joined_default_parameter
- last_where
- legacy_random
- literal_expression_end_indentation
- lower_acl_than_parent
- modifier_order
- multiline_arguments
- multiline_parameters
- multiline_arguments_brackets
- multiline_literal_brackets
- multiline_parameters_brackets
- nslocalizedstring_key
- number_separator
- optional_enum_case_matching
- overridden_super_call
- pattern_matching_keywords
- prefer_self_type_over_type_of_self
- prohibited_super_call
- private_outlet
- private_subject
- private_unit_test
- reduce_into
- redundant_nil_coalescing
- vertical_parameter_alignment_on_call
- untyped_error_in_catch
- empty_string
- empty_xctest_method
- lower_acl_than_parent
- modifier_order
- redundant_objc_attribute
- sorted_first_last
- static_operator
- collection_alignment
- identical_operands
- strong_iboutlet
- toggle_bool
- legacy_random
- trailing_closure
- untyped_error_in_catch
- unused_import
- unused_declaration
- vertical_parameter_alignment_on_call
- vertical_whitespace_between_cases
- redundant_objc_attribute
- multiline_arguments_brackets
- multiline_literal_brackets
- multiline_parameters_brackets
- nslocalizedstring_key
- last_where
- vertical_whitespace_opening_braces
- vertical_whitespace_closing_braces
- strong_iboutlet
- xct_specific_matcher
- trailing_closure
excluded:
- Pods

Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ All notable changes to this project will be documented in this file.
Released on 2022-05-XX

* Support array associations by an arbitrary reference instead of just by ID. This is specified via a new referenceAccessor parameter.
* Updated example to use Codable model
* Updated linting

## [4.0.2](https://github.com/square/FetchRequests/releases/tag/4.0.2)
Released on 2021-12-14
Expand Down

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

2 changes: 1 addition & 1 deletion Example/iOS-Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
rootViewController: ViewController()
)
window?.makeKeyAndVisible()

return true
}
}
41 changes: 23 additions & 18 deletions Example/iOS-Example/Model+Persistence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import FetchRequests

enum ModelError: Error {
case invalidDate
case cannotInflate
}

private let encoder = JSONEncoder()
private let decoder = JSONDecoder()

// MARK: - Event Notifications

extension Model {
Expand Down Expand Up @@ -45,24 +47,22 @@ extension Model {
return UserDefaults.standard.dictionary(forKey: key) ?? [:]
}

fileprivate class func updateStorage(_ block: (inout [String: Any]) -> Void) {
fileprivate class func updateStorage(_ block: (inout [String: Any]) throws -> Void) rethrows {
assert(Thread.isMainThread)

let defaults = UserDefaults.standard
let key = NSStringFromClass(self)

var storage = defaults.dictionary(forKey: key) ?? [:]
block(&storage)
try block(&storage)
defaults.set(storage, forKey: key)
}

private class func validateCanUpdate(_ originalModel: Model) throws -> Model {
var data = originalModel.data.dictionary ?? [:]
data["updatedAt"] = Date().timeIntervalSince1970
var data = originalModel.data
data.updatedAt = Date()

guard let json = JSON(data), let model = self.init(data: json) else {
throw ModelError.cannotInflate
}
let model = Model(data: data)

guard model.createdAt != .distantPast else {
throw ModelError.invalidDate
Expand All @@ -73,8 +73,8 @@ extension Model {
return model
}
guard existing.updatedAt <= model.updatedAt,
existing.createdAt == model.createdAt else
{
existing.createdAt == model.createdAt
else {
throw ModelError.invalidDate
}

Expand All @@ -85,8 +85,9 @@ extension Model {
class func save(_ originalModel: Model) throws {
let model = try validateCanUpdate(originalModel)

updateStorage {
$0[model.id] = model.data.object
try updateStorage {
let data = try encoder.encode(model.data)
$0[model.id] = data
}

NotificationCenter.default.post(
Expand Down Expand Up @@ -124,17 +125,21 @@ extension Model {

extension NSObjectProtocol where Self: Model {
static func fetchAll() -> [Self] {
return storage.values.lazy.compactMap {
Self.RawData($0)
}.compactMap {
return storage.values.lazy.compactMap { value in
value as? Data
}.compactMap { data in
try? decoder.decode(Model.RawData.self, from: data)
}.map {
Self(data: $0)
}
}

static func fetch(byID id: Model.ID) -> Self? {
return storage[id].flatMap {
Self.RawData($0)
}.flatMap {
return storage[id].flatMap { value in
value as? Data
}.flatMap { data in
try? decoder.decode(Model.RawData.self, from: data)
}.map {
Self(data: $0)
}
}
Expand Down
Loading