Skip to content

Commit

Permalink
optimize generator memory
Browse files Browse the repository at this point in the history
  • Loading branch information
hongxinhope committed Jun 6, 2016
1 parent b28955c commit 9aac071
Show file tree
Hide file tree
Showing 9 changed files with 456 additions and 27 deletions.
33 changes: 17 additions & 16 deletions RRuleSwift/Generators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,28 @@ import JavaScriptCore

internal struct Generator {
static let endlessRecurrenceCount = 500
static let rruleContext: JSContext? = {
guard let rrulejs = JavaScriptBridge.rrulejs() else {
return nil
}
let context = JSContext()
context.exceptionHandler = { context, exception in
print("[RRuleSwift] rrule.js error: \(exception)")
}
context.evaluateScript(rrulejs)
return context
}()
}

public extension RecurrenceRule {
public func allOccurrences(endlessRecurrenceCount: Int = Generator.endlessRecurrenceCount) -> [NSDate] {
guard let rrulejs = JavaScriptBridge.rrulejs() else {
guard let _ = JavaScriptBridge.rrulejs() else {
return []
}

let ruleJSONString = toJSONString(endlessRecurrenceCount: endlessRecurrenceCount)
let context = JSContext()
context.exceptionHandler = { context, exception in
print("[RRuleSwift] rrule.js error: \(exception)")
}
context.evaluateScript(rrulejs)
context.evaluateScript("var rule = new RRule({ \(ruleJSONString) })")
guard let allOccurrences = context.evaluateScript("rule.all()").toArray() as? [NSDate] else {
Generator.rruleContext?.evaluateScript("var rule = new RRule({ \(ruleJSONString) })")
guard let allOccurrences = Generator.rruleContext?.evaluateScript("rule.all()").toArray() as? [NSDate] else {
return []
}

Expand All @@ -51,7 +57,7 @@ public extension RecurrenceRule {
}

public func occurrencesBetween(date date: NSDate, andDate otherDate: NSDate, endlessRecurrenceCount: Int = Generator.endlessRecurrenceCount) -> [NSDate] {
guard let rrulejs = JavaScriptBridge.rrulejs() else {
guard let _ = JavaScriptBridge.rrulejs() else {
return []
}

Expand All @@ -61,13 +67,8 @@ public extension RecurrenceRule {
let untilDateJSON = RRule.ISO8601DateFormatter.stringFromDate(untilDate)

let ruleJSONString = toJSONString(endlessRecurrenceCount: endlessRecurrenceCount)
let context = JSContext()
context.exceptionHandler = { context, exception in
print("[RRuleSwift] rrule.js error: \(exception)")
}
context.evaluateScript(rrulejs)
context.evaluateScript("var rule = new RRule({ \(ruleJSONString) })")
guard let betweenOccurrences = context.evaluateScript("rule.between(new Date('\(beginDateJSON)'), new Date('\(untilDateJSON)'))").toArray() as? [NSDate] else {
Generator.rruleContext?.evaluateScript("var rule = new RRule({ \(ruleJSONString) })")
guard let betweenOccurrences = Generator.rruleContext?.evaluateScript("rule.between(new Date('\(beginDateJSON)'), new Date('\(untilDateJSON)'))").toArray() as? [NSDate] else {
return []
}

Expand Down
2 changes: 1 addition & 1 deletion RRuleSwift/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.0.8</string>
<string>0.0.9</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="M4Y-Lb-cyx">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
</dependencies>
<scenes>
<!--Today View Controller-->
<scene sceneID="cwh-vc-ff4">
<objects>
<viewController id="M4Y-Lb-cyx" customClass="TodayViewController" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Ft6-oW-KC0"/>
<viewControllerLayoutGuide type="bottom" id="FKl-LY-JtV"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" simulatedAppContext="notificationCenter" id="S3S-Oj-5AN">
<rect key="frame" x="0.0" y="0.0" width="320" height="37"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="top" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Hello World" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="280" translatesAutoresizingMaskIntoConstraints="NO" id="GcN-lo-r42">
<rect key="frame" x="20" y="8" width="280" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="lightTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="FKl-LY-JtV" firstAttribute="top" secondItem="GcN-lo-r42" secondAttribute="bottom" constant="20" symbolic="YES" id="0Q0-KW-PJ6"/>
<constraint firstItem="GcN-lo-r42" firstAttribute="leading" secondItem="S3S-Oj-5AN" secondAttribute="leading" constant="20" symbolic="YES" id="6Vq-gs-PHe"/>
<constraint firstAttribute="trailing" secondItem="GcN-lo-r42" secondAttribute="trailing" constant="20" symbolic="YES" id="L8K-9R-egU"/>
<constraint firstItem="GcN-lo-r42" firstAttribute="top" secondItem="Ft6-oW-KC0" secondAttribute="bottom" constant="20" symbolic="YES" id="mYS-Cv-VNx"/>
</constraints>
</view>
<extendedEdge key="edgesForExtendedLayout"/>
<nil key="simulatedStatusBarMetrics"/>
<nil key="simulatedTopBarMetrics"/>
<nil key="simulatedBottomBarMetrics"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<size key="freeformSize" width="320" height="37"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="vXp-U4-Rya" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="516" y="285"/>
</scene>
</scenes>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar"/>
<simulatedOrientationMetrics key="orientation"/>
<simulatedScreenMetrics key="destination" type="retina4"/>
</simulatedMetricsContainer>
</document>
33 changes: 33 additions & 0 deletions RRuleSwiftExample-TodayExtension/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>RRuleSwiftExample-TodayExtension</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>0.0.9</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>9</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.widget-extension</string>
</dict>
</dict>
</plist>
76 changes: 76 additions & 0 deletions RRuleSwiftExample-TodayExtension/TodayViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// TodayViewController.swift
// RRuleSwiftExample-TodayExtension
//
// Created by 洪鑫 on 16/6/6.
// Copyright © 2016年 Teambition. All rights reserved.
//

import UIKit
import NotificationCenter
import RRuleSwift

class TodayViewController: UIViewController, NCWidgetProviding {
let dateFormatter: NSDateFormatter = {
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone.defaultTimeZone()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss EEE"
return dateFormatter
}()

override func viewDidLoad() {
super.viewDidLoad()
var rule = RecurrenceRule(recurrenceWithRRuleString: "RRULE:FREQ=WEEKLY;DTSTART=20151119T014500Z;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR;UNTIL=20170101T014500Z")!
rule.exdate = ExclusionDate(exdateString: "EXDATE:20151120T014500Z,20151123T014500Z,20151126T014500Z,20151127T014500Z,20151130T014500Z,20151201T014500Z,20151202T014500Z,20151203T014500Z,20151207T014500Z,20151210T014500Z,20151211T014500Z,20151215T014500Z,20151216T014500Z,20151217T014500Z,20151221T014500Z,20151222T014500Z,20151223T014500Z,20151228T014500Z,20151229T014500Z", unitGranularity: .Day)
let date1 = dateFormatter.dateFromString("2016-06-06 00:00:00 Sun")!
let date2 = dateFormatter.dateFromString("2016-12-13 00:00:00 Sun")!

let occurrences1 = rule.occurrencesBetween(date: date1, andDate: date2)
let occurrences2 = rule.occurrencesBetween(date: date1, andDate: date2)
let occurrences3 = rule.occurrencesBetween(date: date1, andDate: date2)
let occurrences4 = rule.occurrencesBetween(date: date1, andDate: date2)
let occurrences5 = rule.occurrencesBetween(date: date1, andDate: date2)
let occurrences6 = rule.occurrencesBetween(date: date1, andDate: date2)
let occurrences7 = rule.occurrencesBetween(date: date1, andDate: date2)
let occurrences8 = rule.occurrencesBetween(date: date1, andDate: date2)
let occurrences9 = rule.occurrencesBetween(date: date1, andDate: date2)
let occurrences10 = rule.occurrencesBetween(date: date1, andDate: date2)
let _ = rule.occurrencesBetween(date: date1, andDate: date2)
let _ = rule.occurrencesBetween(date: date1, andDate: date2)
let _ = rule.occurrencesBetween(date: date1, andDate: date2)
let _ = rule.occurrencesBetween(date: date1, andDate: date2)
let _ = rule.occurrencesBetween(date: date1, andDate: date2)
let _ = rule.occurrencesBetween(date: date1, andDate: date2)
let _ = rule.occurrencesBetween(date: date1, andDate: date2)
let _ = rule.occurrencesBetween(date: date1, andDate: date2)
let _ = rule.occurrencesBetween(date: date1, andDate: date2)
let _ = rule.occurrencesBetween(date: date1, andDate: date2)

print(occurrences1)
print(occurrences2)
print(occurrences3)
print(occurrences4)
print(occurrences5)
print(occurrences6)
print(occurrences7)
print(occurrences8)
print(occurrences9)
print(occurrences10)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}

func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
// Perform any setup necessary in order to update the view.

// If an error is encountered, use NCUpdateResult.Failed
// If there's no update required, use NCUpdateResult.NoData
// If there's an update, use NCUpdateResult.NewData

completionHandler(NCUpdateResult.NewData)
}
}
Loading

0 comments on commit 9aac071

Please sign in to comment.