Skip to content

Commit

Permalink
1. clearCache is now called right after memory warning notification h…
Browse files Browse the repository at this point in the history
…as been send. A note about this is added to the .h

2. Tests are added.
2.1 checking that clearCache calls are safe, while there is a recognition is in process. A note about this is also added to .h
2.2 checking that clearCache is automatically called on UIApplicationDidReceiveMemoryWarningNotification
  • Loading branch information
Kirill Makankov committed Jan 12, 2015
1 parent b902703 commit b2355de
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion TesseractOCR/G8Tesseract.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
* Clear any library-level memory caches.
* There are a variety of expensive-to-load constant data structures (mostly
* language dictionaries) that are cached globally. This function allows the
* clearing of these caches.
* clearing of these caches. It's safe to call this method, while a recognition is in progress.
* It's also called automatically on UIApplicationDidReceiveMemoryWarningNotification
*/
+ (void)clearCache;

Expand Down
16 changes: 16 additions & 0 deletions TesseractOCR/G8Tesseract.mm
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ @implementation G8Tesseract

@synthesize absoluteDataPath=_absoluteDataPath;

+ (void)initialize {

[super initialize];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didReceiveMemoryWarningNotification:)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
}

+ (void)didReceiveMemoryWarningNotification:(NSNotification*)notification {

[self clearCache];
// some more cleaning here if necessary
}

+ (NSString *)version
{
const char *version = tesseract::TessBaseAPI::Version();
Expand Down
21 changes: 21 additions & 0 deletions TestsProject/TestsProjectTests/InitializationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "Defaults.h"

@interface G8Tesseract (Tests)
+ (void)didReceiveMemoryWarningNotification:(NSNotification*)notification;
- (BOOL)configEngine;
- (BOOL)resetEngine;
@end
Expand Down Expand Up @@ -53,6 +54,26 @@ - (BOOL)resetEngine;
it(@"Should check version", ^{
[[[G8Tesseract version] should] equal:@"3.03"];
});

it(@"Should clear cache", ^{
// while recognition is in progress
for (int i = 0; i <= 10; i++) {
G8RecognitionOperation *operation = [[G8RecognitionOperation alloc] init];
operation.tesseract.image = [UIImage imageNamed:@"well_scaned_page"];
operation.tesseract.language = kG8Languages;

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];
}

[[theBlock(^{
[G8Tesseract clearCache];
}) shouldNot] raise];

// should be called on a memory warning notification
[[G8Tesseract should] receive:@selector(didReceiveMemoryWarningNotification:)];
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidReceiveMemoryWarningNotification object:nil];
});
});

context(@"nil cachesRelatedDataPath", ^{
Expand Down

0 comments on commit b2355de

Please sign in to comment.