Skip to content

Commit 98dd700

Browse files
committed
Adding method that allows one to fetch the previous versionTag that was being used by a YapDatabaseView (or view subclass) during last app run.
1 parent 521e091 commit 98dd700

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

YapDatabase/Extensions/Views/YapDatabaseView.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,26 @@ __attribute((deprecated("Use method initWithGrouping:sorting:versionTag:options:
127127
**/
128128
@property (nonatomic, copy, readonly) YapDatabaseViewOptions *options;
129129

130+
/**
131+
* Allows you to fetch the versionTag from a view that was registered during the last app launch.
132+
*
133+
* For example, let's say you have a view that sorts contacts.
134+
* And you support 2 different sort options:
135+
* - First, Last
136+
* - Last, First
137+
*
138+
* To support this, you use 2 different versionTags:
139+
* - "First,Last"
140+
* - "Last,First"
141+
*
142+
* And you want to ensure that when you first register the view (during app launch),
143+
* you choose the same block & versionTag from a previous app launch (if possible).
144+
* This prevents the view from enumerating the database & re-populating itself
145+
* during registration if the versionTag is different from last time.
146+
*
147+
* So you can use this method to fetch the previous versionTag.
148+
**/
149+
+ (NSString *)previousVersionTagForRegisteredViewName:(NSString *)name
150+
withTransaction:(YapDatabaseReadTransaction *)transaction;
151+
130152
@end

YapDatabase/Extensions/Views/YapDatabaseView.m

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,62 @@ + (NSString *)pageMetadataTableNameForRegisteredName:(NSString *)registeredName
8585
return [NSString stringWithFormat:@"view_%@_pageMetadata", registeredName];
8686
}
8787

88+
/**
89+
* Allows you to fetch the versionTag from a view that was registered during the last app launch.
90+
*
91+
* For example, let's say you have a view that sorts contacts.
92+
* And you support 2 different sort options:
93+
* - First, Last
94+
* - Last, First
95+
*
96+
* To support this, you use 2 different versionTags:
97+
* - "First,Last"
98+
* - "Last,First"
99+
*
100+
* And you want to ensure that when you first register the view (during app launch),
101+
* you choose the same block & versionTag from a previous app launch (if possible).
102+
* This prevents the view from enumerating the database & re-populating itself
103+
* during registration if the versionTag is different from last time.
104+
*
105+
* So you can use this method to fetch the previous versionTag.
106+
**/
107+
+ (NSString *)previousVersionTagForRegisteredViewName:(NSString *)registeredName
108+
withTransaction:(YapDatabaseReadTransaction *)transaction
109+
{
110+
NSString *prevVersionTag = [transaction stringValueForKey:ext_key_versionTag extension:registeredName];
111+
112+
if (prevVersionTag == nil)
113+
{
114+
NSString *prevClassName = [transaction stringValueForKey:ext_key_class extension:registeredName];
115+
116+
if ([prevClassName isEqualToString:@"YapDatabaseFilteredView"])
117+
{
118+
NSString *prevTag_deprecated =
119+
[transaction stringValueForKey:ext_key_tag_deprecated extension:registeredName];
120+
121+
if (prevTag_deprecated)
122+
{
123+
prevVersionTag = prevTag_deprecated;
124+
}
125+
}
126+
else
127+
{
128+
int prevVersion_deprecated = 0;
129+
BOOL hasPrevVersion_deprecated = [transaction getIntValue:&prevVersion_deprecated
130+
forKey:ext_key_version_deprecated
131+
extension:registeredName];
132+
133+
if (hasPrevVersion_deprecated)
134+
{
135+
prevVersionTag = [NSString stringWithFormat:@"%d", prevVersion_deprecated];
136+
}
137+
}
138+
139+
}
140+
141+
return prevVersionTag;
142+
}
143+
88144
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
89145
#pragma mark Instance
90146
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)