Skip to content

Commit 4d29e39

Browse files
committed
Added record deletion if number of records in entity exceeds Maximum Records
1 parent 5e18783 commit 4d29e39

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

Swift Push/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import UIKit
1010
import CoreData
1111

12-
var maximumRecords = 1000
12+
var maximumRecords = 500
1313

1414

1515
@UIApplicationMain

Swift Push/Base.lproj/Main.storyboard

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="15C50" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="XCN-vs-9xr">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="15D9c" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="XCN-vs-9xr">
33
<dependencies>
44
<deployment identifier="iOS"/>
55
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
@@ -15,21 +15,21 @@
1515
<color key="backgroundColor" red="0.93725490199999995" green="0.93725490199999995" blue="0.95686274510000002" alpha="1" colorSpace="calibratedRGB"/>
1616
<prototypes>
1717
<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Cell" textLabel="Y6V-pK-YF5" detailTextLabel="QOP-pJ-6oa" style="IBUITableViewCellStyleSubtitle" id="mta-ed-FwN">
18-
<rect key="frame" x="0.0" y="114" width="600" height="44"/>
18+
<rect key="frame" x="0.0" y="113.5" width="600" height="44"/>
1919
<autoresizingMask key="autoresizingMask"/>
2020
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="mta-ed-FwN" id="WoY-P6-9QE">
21-
<rect key="frame" x="0.0" y="0.0" width="600" height="43"/>
21+
<rect key="frame" x="0.0" y="0.0" width="600" height="43.5"/>
2222
<autoresizingMask key="autoresizingMask"/>
2323
<subviews>
2424
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="Y6V-pK-YF5">
25-
<rect key="frame" x="15" y="5" width="32" height="20"/>
25+
<rect key="frame" x="15" y="6" width="31.5" height="19.5"/>
2626
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
2727
<fontDescription key="fontDescription" type="system" pointSize="16"/>
2828
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
2929
<nil key="highlightedColor"/>
3030
</label>
3131
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Subtitle" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="QOP-pJ-6oa">
32-
<rect key="frame" x="15" y="25" width="41" height="14"/>
32+
<rect key="frame" x="15" y="25.5" width="40.5" height="13.5"/>
3333
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
3434
<fontDescription key="fontDescription" type="system" pointSize="11"/>
3535
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>

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>346</string>
22+
<string>356</string>
2323
<key>LSRequiresIPhoneOS</key>
2424
<true/>
2525
<key>UIBackgroundModes</key>

Swift Push/ViewController.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,25 @@ func newRecord (messageText: String, alert: Bool) {
217217
do {
218218
let records = try context.executeFetchRequest(fetch)
219219
if records.count > maximumRecords {
220+
// if we have too many reorcrds, delete them down to MaximumRecords
220221
print ("too many records (\(records.count) of \(maximumRecords))")
222+
223+
let deleteFetchRequest = NSFetchRequest(entityName: "PushMessages")
224+
deleteFetchRequest.fetchLimit = records.count - maximumRecords
225+
226+
let sortDescriptor = NSSortDescriptor(key: "timeReceived", ascending: true)
227+
deleteFetchRequest.sortDescriptors = [sortDescriptor]
228+
229+
do {
230+
let deleteFetchResults = try context.executeFetchRequest (deleteFetchRequest)
231+
232+
for var delRecord in deleteFetchResults {
233+
context.deleteObject(delRecord as! NSManagedObject)
234+
}
235+
} catch {
236+
let fetchError = error as NSError
237+
print("\(fetchError), \(fetchError.userInfo)")
238+
}
221239
}
222240
} catch {
223241
let fetchError = error as NSError

0 commit comments

Comments
 (0)