• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

optimizely / swift-sdk / 4670339270

pending completion
4670339270

Pull #486

github

GitHub
Merge 88f08851e into 8b43600a3
Pull Request #486: [FSSDK-9062] Jae/empty odp action

2 of 2 new or added lines in 1 file covered. (100.0%)

701 of 736 relevant lines covered (95.24%)

5831.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/Sources/Implementation/Datastore/DataStoreUserDefaults.swift
1
//
2
// Copyright 2019-2022, Optimizely, Inc. and contributors
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
//
8
//    http://www.apache.org/licenses/LICENSE-2.0
9
//
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15
//
16

17
import Foundation
18

19
/// Implementation of OPTDataStore using standard UserDefaults.
20
/// This class should be used as a singleton.
21
public class DataStoreUserDefaults: OPTDataStore {
22
    // A hardcoded max for user defaults.  Since there is a max on iostv
23
    #if os(tvOS)
24
    static let MAX_DS_SIZE = 128000
25
    #else
26
    static let MAX_DS_SIZE = 1000000
27
    #endif
28
    static let dispatchQueue = DispatchQueue(label: "OPTDataStoreQueueUserDefaults")
29

30
    // thread-safe lazy logger load (after HandlerRegisterService ready)
31
    private let threadSafeLogger = ThreadSafeLogger()
785✔
32
    var logger: OPTLogger {
1✔
33
        return threadSafeLogger.logger
34
    }
35

36
    public func getItem(forKey: String) -> Any? {
2,303✔
37
        
38
        return DataStoreUserDefaults.dispatchQueue.sync {
2,302✔
39
            return UserDefaults.standard.object(forKey: forKey)
40
        }
41
    }
42
    
43
    public func saveItem(forKey: String, value: Any) {
1,839✔
44

45
        DataStoreUserDefaults.dispatchQueue.async {
1,838✔
46
            if let value = value as? Data {
47
                if value.count > DataStoreUserDefaults.MAX_DS_SIZE {
48
                    self.logger.e("Save to User Defaults error: \(forKey) is too big to save size(\(value.count))")
49
                    return
50
                }
51
            } else if let value = value as? String {
52
                if value.count > DataStoreUserDefaults.MAX_DS_SIZE {
53
                    self.logger.e("Save to User Defaults error: \(forKey) is too big to save size(\(value.count))")
54
                    return
55
                }
56
            } else if let value = value as? [Data] {
57
                var l: Int = 0
58
                l = value.reduce(into: l, { (res, data) in
1,000,004✔
59
                    res += data.count
60
                })
61
                if l > DataStoreUserDefaults.MAX_DS_SIZE {
62
                    self.logger.e("Save to User Defaults error: \(forKey) is too big to save size(\(value.count))")
63
                    return
64
                }
65
            } else if let value = value as? [String] {
66
                var l: Int = 0
67
                l = value.reduce(into: l, { (res, data) in
×
68
                    res += data.count
69
                })
70
                if l > DataStoreUserDefaults.MAX_DS_SIZE {
71
                    self.logger.e("Save to User Defaults error: \(forKey) is too big to save size(\(value.count))")
72
                    return
73
                }
74
            }
75
            UserDefaults.standard.set(value, forKey: forKey)
76
            UserDefaults.standard.synchronize()
77
        }
78
    }
79
    
80
    public func removeItem(forKey: String) {
3✔
81
        UserDefaults.standard.removeObject(forKey: forKey)
82
    }
83
    
84
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc