Skip to content

Commit c795e45

Browse files
committed
Add check for maximum number of records exceeded.
1 parent 1e71e12 commit c795e45

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

Swift Push/AppDelegate.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import UIKit
1010
import CoreData
1111

12+
var maximumRecords = 1000
13+
14+
1215
@UIApplicationMain
1316
class AppDelegate: UIResponder, UIApplicationDelegate {
1417

@@ -28,7 +31,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2831
let versionNumber: AnyObject? = NSBundle.mainBundle().infoDictionary?["CFBundleVersion"]
2932
print ("version \(versionNumber!)")
3033

31-
3234
// Fetch Main Storyboard
3335
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
3436

@@ -46,7 +48,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
4648
let fetch = NSFetchRequest (entityName: "PushMessages")
4749
do {
4850
let records = try self.managedObjectContext.executeFetchRequest(fetch)
49-
newRecord("Swift Push (\(versionNumber!)) starting on " + UIDevice.currentDevice().name + " with \(records.count) records", alert: true)
51+
newRecord("Swift Push (\(versionNumber!)) starting on " + UIDevice.currentDevice().name + " with \(records.count) of \(maximumRecords) records", alert: true)
5052
} catch {
5153
let fetchError = error as NSError
5254
print("\(fetchError), \(fetchError.userInfo)")

Swift Push/Base.lproj/Main.storyboard

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" pagingEnabled="YES" dataMode="prototypes" style="grouped" separatorStyle="default" rowHeight="44" sectionHeaderHeight="18" sectionFooterHeight="18" id="A2o-jL-Vhq">
1313
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
1414
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
15-
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
15+
<color key="backgroundColor" red="0.93725490199999995" green="0.93725490199999995" blue="0.95686274510000002" alpha="1" colorSpace="calibratedRGB"/>
1616
<prototypes>
1717
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Cell" textLabel="Y6V-pK-YF5" detailTextLabel="QOP-pJ-6oa" style="IBUITableViewCellStyleSubtitle" id="mta-ed-FwN">
1818
<rect key="frame" x="0.0" y="113.5" width="600" height="44"/>

Swift Push/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>
22-
<string>304</string>
22+
<string>316</string>
2323
<key>LSRequiresIPhoneOS</key>
2424
<true/>
2525
<key>UIBackgroundModes</key>

Swift Push/ViewController.swift

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,11 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
9797
cell.detailTextLabel!.numberOfLines = 0
9898
cell.detailTextLabel!.text = NSDateFormatter.localizedStringFromDate((record.valueForKey("timeReceived") as? NSDate)!, dateStyle: .MediumStyle, timeStyle: .ShortStyle) as String
9999

100-
//if ((record.valueForKey("isAlert") ?? false) != nil) {
101-
// cell.backgroundColor = UIColor.lightGrayColor().colorWithAlphaComponent(0.2)
102-
//}
103-
104-
if let value = record.valueForKey("isAlert") {
105-
if (value as! Bool) {
106-
cell.textLabel!.font = cell.textLabel!.font.boldItalic()
107-
//cell.backgroundColor = UIColor.lightGrayColor()
108-
print ("true")
109-
} else{
110-
print ("false")
111-
}
112-
}
100+
// if let value = record.valueForKey("isAlert") {
101+
// if (value as! Bool) {
102+
// cell.textLabel!.font = cell.textLabel!.font.boldItalic()
103+
// }
104+
// }
113105
}
114106

115107
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
@@ -200,6 +192,19 @@ func newRecord (messageText: String, alert: Bool) {
200192
record.setValue(NSDate(), forKey: "timeReceived")
201193
record.setValue(alert, forKey: "isAlert")
202194

195+
// check we don't have too many records
196+
let fetch = NSFetchRequest (entityName: "PushMessages")
197+
do {
198+
let records = try context.executeFetchRequest(fetch)
199+
if records.count > maximumRecords {
200+
print ("too many records (\(records.count) of \(maximumRecords))")
201+
}
202+
} catch {
203+
let fetchError = error as NSError
204+
print("\(fetchError), \(fetchError.userInfo)")
205+
}
206+
207+
// save the changed table
203208
do {
204209
try record.managedObjectContext?.save()
205210
} catch {

0 commit comments

Comments
 (0)