Limit clipboard history to about 10,000 items

Once we reach the threshold we delete half the history to ensure a good amortized time complexity.
This commit is contained in:
Allan Odgaard
2014-02-20 10:34:49 +07:00
parent 1f74cef1bd
commit 520084a08c

View File

@@ -428,6 +428,13 @@ static NSMutableDictionary* SharedInstances = [NSMutableDictionary new];
if(createNewEntry)
{
static NSInteger const kHistorySize = 10000;
if([self.entries count] > kHistorySize)
{
for(OakPasteboardEntry* entry in [self.entries objectsAtIndexes:[[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, [self.entries count] - kHistorySize/2)]])
[entry.managedObjectContext deleteObject:entry];
}
NSMutableOrderedSet* entries = [self mutableOrderedSetValueForKey:@"entries"];
[entries addObject:[OakPasteboardEntry pasteboardEntryWithString:aString andOptions:someOptions inContext:self.managedObjectContext]];
}