How to find the recently viewed records using objective C - objective-c

Im working on recently viewed functionality, I have to create one model class of History. In history i have FileName,Title,ActivityName. Im using 2 methods to store the filename and title.
History.h
-(BOOL)history:(NSString *)sFile activityName:(NSString *)activityName title:(NSString *)title GID_ID:(NSString *)dataGID_ID;
-(id)getHistoryInstance;
-(BOOL)addNewHistory:(Histrory *)history;
-(NSMutableArray *)recentlyViewedDict;
History.m
+(Histrory *)getSharedInstance{
if (!sharedInstance) {
sharedInstance = [[super allocWithZone:NULL]init];
[sharedInstance recentlyViewedDict];
}
return sharedInstance;
}
-(NSMutableArray *)recentlyViewedDict {
return recentlyViewedDict;
}
-(BOOL)history:(NSString *)sFile activityName:(NSString *)activityName title:(NSString *)title GID_ID:(NSString *)dataGID_ID
{
_sFile = sFile;
_titleName = title ;
_dataGID_ID = dataGID_ID;
_activityName = activityName;
[self performSelector:#selector(addNewHistory:) withObject:self afterDelay:0.1];
return YES;
}
-(id)getHistoryInstance {
NSLog(#"RECENTLY VIEWED DICY %#", recentlyViewedDict);
if ([recentlyViewedDict count] == 0) {
recentlyViewedDict = [[NSMutableArray alloc]init];
}
else {
return recentlyViewedDict;
}
return recentlyViewedDict;
}
-(BOOL)addNewHistory:(Histrory *)his {
BOOL val = false;
recentlyViewedDict = [self getHistoryInstance];
if ([recentlyViewedDict count] != 0) {
for (int i = 0 ; i < [recentlyViewedDict count]; i++) {
Histrory *one = (Histrory *) recentlyViewedDict[i];
if ([his.getData_GIDID isEqualToString:one.getData_GIDID]) {
val = false;
[recentlyViewedDict removeObjectAtIndex:i];
}
}
}
else {
NSString *addData = [NSString stringWithFormat:#"%#,%#,%#,%#",his.getsFile,his.getTitle,his.getAcivityName,his.getData_GIDID];
[recentlyViewedDict addObject:addData];
}
if ([recentlyViewedDict count] > 11) {
[recentlyViewedDict removeObjectAtIndex:0];
}
NSLog(#"Recently Viewed %#", recentlyViewedDict);
return YES;
}
In this every time i have to store only 1 file.I want to store at least 10 records, if 11 record inserting then 1 record will be deleted using below method.
if ([recentlyViewedDict count] > 11) {
[recentlyViewedDict removeObjectAtIndex:0];
}
How can i store each time recent record.
ContactDetails.m
historyObj = [[Histrory alloc]init];
[historyObj history:sFile activityName:activityName title:contactName GID_ID:contactGID_ID];
Im calling this method every time to visit details classes. this method works only one time like, it will store only first record, you are visited second time it is showing nil value. How can store the multiple detail classes to store each record.thanks in advance.

Related

FFmpeg jump to most recent frame

I am looking for some help with dropping/skipping FFmpeg frames. The project I am working on streams live video which when the app goes into the background, upon returning to an active state the video stream spends a long time catching up by fast forwarding itself to the current frame. This isn't ideal and what I am aiming to achieve is have the app immediately jump to the most recent frame.
What I need to do is drop the amount of frames that are being fast-forwarded in order to catch up to the most recent frame. Is this possible? Here is my current code which decodes the frames:
- (NSArray *) decodeFrames: (CGFloat) minDuration
{
NSMutableArray *result = [NSMutableArray array];
#synchronized (lock) {
if([_reading integerValue] != 1){
_reading = [NSNumber numberWithInt:1];
#synchronized (_seekPosition) {
if([_seekPosition integerValue] != -1 && _seekPosition){
[self seekDecoder:[_seekPosition longLongValue]];
_seekPosition = [NSNumber numberWithInt:-1];
}
}
if (_videoStream == -1 &&
_audioStream == -1)
return nil;
AVPacket packet;
CGFloat decodedDuration = 0;
CGFloat totalDuration = [TimeHelper calculateTimeDifference];
do {
BOOL finished = NO;
int count = 0;
while (!finished) {
if (av_read_frame(_formatCtx, &packet) < 0) {
_isEOF = YES;
[self endOfFileReached];
break;
}
[self frameRead];
if (packet.stream_index ==_videoStream) {
int pktSize = packet.size;
while (pktSize > 0) {
int gotframe = 0;
int len = avcodec_decode_video2(_videoCodecCtx,
_videoFrame,
&gotframe,
&packet);
if (len < 0) {
LoggerVideo(0, #"decode video error, skip packet");
break;
}
if (gotframe) {
if (!_disableDeinterlacing &&
_videoFrame->interlaced_frame) {
avpicture_deinterlace((AVPicture*)_videoFrame,
(AVPicture*)_videoFrame,
_videoCodecCtx->pix_fmt,
_videoCodecCtx->width,
_videoCodecCtx->height);
}
KxVideoFrame *frame = [self handleVideoFrame];
if (frame) {
[result addObject:frame];
_position = frame.position;
decodedDuration += frame.duration;
if (decodedDuration > minDuration)
finished = YES;
}
} else {
count++;
}
if (0 == len)
break;
pktSize -= len;
}
}
av_free_packet(&packet);
}
} while (totalDuration > 0);
_reading = [NSNumber numberWithInt:0];
return result;
}
}
return result;

How to check if an object is the last object on NSArray

How can I check if an object in the last object on an NSArray?
I've tried:
if ([currentStore isEqual:[Stores lastObject]])
{
//Code
}
but it didn't work.
Any idea?
Thanks!
or try this
BOOL lastElement = false;
NSUInteger index = [stores indexOfObject:currentStore];
if (index != NSNotFound)
{
lastElement = (index == [stores count] - 1);
}
Bit modified try this:
NSUInteger index = [stores indexOfObject:currentStore];
if (index == ([stores count]-1))
{
NSLog(#"Yes its Last");
}
If you didn't override isEqual method, the base class implementation of NSObject::isEqual only check if both pointers points to the same address.
This excellent article http://nshipster.com/equality/ explain objc equality principles
The below sample logs - Testing Stores - works fine
#interface Stores : NSObject
#property (strong, nonatomic) NSString* name;
- (instancetype) initWithName:(NSString*) name;
#end
#implementation Stores
- (instancetype) initWithName:(NSString*) name;
{
_name = name;
return self;
}
- (BOOL)isEqualToStores:(Stores*) Stores
{
if (!Stores)
return NO;
if (![_name isEqualToString:Stores.name] )
return NO;
return YES;
}
- (BOOL)isEqual:(id)object
{
if (self == object)
{
return YES;
}
if (![object isKindOfClass:[Stores class]])
{
return NO;
}
return [self isEqualToStores:(Stores *)object];
}
#end
-(void) testStores
{
Stores* last = [[Stores alloc] initWithName:#"5"];
NSArray* arr = #[
[[Stores alloc] initWithName:#"1"],
[[Stores alloc] initWithName:#"2"],
[[Stores alloc] initWithName:#"3"],
[[Stores alloc] initWithName:#"4"],
[[Stores alloc] initWithName:#"5"]
//last
];
if ([last isEqual:[arr lastObject]])
{
NSLog(#"Testing Stores - works fine");
}
else
{
NSLog(#"Testing Stores - opps!?1?!?");
}
}

Always returning null in NSString return function

I have the following code where I want to convert decimal odds to fractional odds. However the function findNearestWholeInteger always returns null.
- (NSString *)displayOddWithFormat:(NSString *)decimalOdd {
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"FractionalOdds"] == YES) {
float oddsF = decimalOdd.floatValue;
oddsF = oddsF - 1.0f;
return [self findNearestWholeInteger:oddsF andInitial:oddsF andBottom:1];
} else {
return odd;
}
}
- (NSString *)findNearestWholeInteger:(float)odds andInitial:(float)initial andBottom:(float)bottom {
NSNumber *numberValue = [NSNumber numberWithFloat:odds];
NSString *floatString = [numberValue stringValue];
NSArray *floatStringComps = [floatString componentsSeparatedByString:#"."];
if (floatStringComps.count == 1) {
return [NSString stringWithFormat:#"%.f/%.f", odds, bottom];
} else {
bottom += 1;
odds += initial;
[self findNearestWholeInteger:odds andInitial:initial andBottom:bottom];
return nil;
}
}
Any ideas where I need to adapt my code? Thanks in advance!
Don't you want:
return [self findNearestWholeInteger:odds andInitial:initial andBottom:bottom];
//return nil;
(not that I really understand what the method is doing).

(Objective-c/Mac OSX) How to distinguish managed AD users (AD user create mobile card) from local users on Mac OSX

<\RESOLVED>, Please see the first reply
My mac(10.9) has joined into a AD domain. In my program, I tried to recognize whether the current login user is local account or AD user. I can successfully distinguish them by using the following code.
+ (bool)isLocalUser:(NSString*)user
{
NSError *dirSearchError = nil;
ODRecord *foundUser = findUser(user, &dirSearchError);
if(foundUser !=nil)
{
return YES;
}else
{
return NO;
}
}
ODRecord *findUser(NSString *user, NSError **error)
{
NSLog(#"[MacLogonUI] findUser");
ODNode *searchNode = [ODNode nodeWithSession: [ODSession defaultSession]
type: kODNodeTypeLocalNodes
error: error];
if (searchNode == nil) {
return nil;
}
NSDictionary *nodeInfo = [searchNode nodeDetailsForKeys:nil error:error];
/* query this node for the user record we're interested in.
* We only need one result, which is why maximumResults is set to 1.
*/
ODQuery *userSearch = [ODQuery queryWithNode: searchNode
forRecordTypes: kODRecordTypeUsers
attribute: kODAttributeTypeRecordName
matchType: kODMatchEqualTo
queryValues: user
returnAttributes: kODAttributeTypeStandardOnly
maximumResults: 1
error: error];
if (userSearch == nil) {
return nil;
}
/* For this example we'll use a synchronous search. This could take a while
* so asynchronous searching is preferable.
*/
NSArray *foundRecords = [userSearch resultsAllowingPartial: NO error: error];
if (foundRecords == nil || [foundRecords count] == 0) {
return nil;
}
ODRecord *userRecord = [foundRecords objectAtIndex: 0];
return [[userRecord retain] autorelease];
}
While when the AD user create a mobile card, it is viewed as a managed user(from the System preference -> Users & Groups). The code also recognize this kind of AD user as local. How to deal with this kind of situation?
Do you guys have any idea of this problem?
I have solved this problem by myself. Hope the following code helps:
#import "DasUser.h"
#import <OpenDirectory/OpenDirectory.h>
#import <Collaboration/Collaboration.h>
#implementation DasUser
+ (bool)isLocalUser:(NSString*)user
{
NSError *dirSearchError = nil;
ODRecord *foundUser = findUser(user, &dirSearchError);
if(foundUser !=nil)
{
return YES;
}else
{
return NO;
}
}
ODRecord *findUser(NSString *user, NSError **error)
{
NSLog(#"[MacLogonUI] findUser");
CSIdentityAuthorityRef defaultAuthority = CSGetManagedIdentityAuthority();
CSIdentityClass identityClass = kCSIdentityClassUser;
CSIdentityQueryRef query = CSIdentityQueryCreate(NULL, identityClass, defaultAuthority);
CFErrorRef err = NULL;
CSIdentityQueryExecute(query, 0, &err);
CFArrayRef results = CSIdentityQueryCopyResults(query);
int numResults = CFArrayGetCount(results);
NSMutableArray * managedUsers = [NSMutableArray array];
for (int i = 0; i < numResults; ++i) {
CSIdentityRef identity = (CSIdentityRef)CFArrayGetValueAtIndex(results, i);
CBIdentity * identityObject = [CBIdentity identityWithCSIdentity:identity];
NSString* posixName = [identityObject posixName];
[managedUsers addObject:posixName];
}
CFRelease(results);
CFRelease(query);
ODNode *searchNode = [ODNode nodeWithSession: [ODSession defaultSession]
type: kODNodeTypeLocalNodes
error: error];
if (searchNode == nil) {
return nil;
}
/* query this node for the user record we're interested in.
* We only need one result, which is why maximumResults is set to 1.
*/
ODQuery *userSearch = [ODQuery queryWithNode: searchNode
forRecordTypes: kODRecordTypeUsers
attribute: kODAttributeTypeRecordName
matchType: kODMatchEqualTo
queryValues: user
returnAttributes: kODAttributeTypeStandardOnly
maximumResults: 1
error: error];
if (userSearch == nil) {
return nil;
}
/* For this example we'll use a synchronous search. This could take a while
* so asynchronous searching is preferable.
*/
NSArray *foundRecords = [userSearch resultsAllowingPartial: NO error: error];
if([foundRecords count]>0)
{
NSString *nameStr = [foundRecords[0] recordName];
NSLog(#"[MacLogonUI] findUser nameStr %#", nameStr);
int j;
for( j = 0; j<[managedUsers count]; j++)
{
if([nameStr isEqualToString:managedUsers[j]])
{
break;
}
}
if(j<[managedUsers count])
{
foundRecords = nil;
}
}
if (foundRecords == nil || [foundRecords count] == 0) {
return nil;
}
ODRecord *userRecord = [foundRecords objectAtIndex: 0];
return [[userRecord retain] autorelease];
}
#end
While when network of the mac is disconnected. The managed user can not be listed. Is there anybody has any idea of this?

iOS: Best Way to do This w/o Calling Method 32 Times?

I'm currently retrieving the Top 100 Scores for one of my leaderboards the following way:
- (void) retrieveTop100Scores {
__block int totalScore = 0;
GKLeaderboard *myLB = [[GKLeaderboard alloc] init];
myLB.identifier = [Team currentTeam];
myLB.timeScope = GKLeaderboardTimeScopeAllTime;
myLB.playerScope = GKLeaderboardPlayerScopeGlobal;
myLB.range = NSMakeRange(1, 100);
[myLB loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
if (error != nil) {
NSLog(#"%#", [error localizedDescription]);
}
if (scores != nil) {
for (GKScore *score in scores) {
NSLog(#"%lld", score.value);
totalScore += score.value;
}
NSLog(#"Total Score: %d", totalScore);
[self loadingDidEnd];
}
}];
}
The problem is I want to do this for 32 leaderboards. I have an array with all 32 leaderboard identifiers:
NSArray *leaderboardIDs;
So my question is, how can I combine those 2 segments of code to pull in the 100 values for each leaderboard, resulting in a dictionary with all of the leaderboard names as keys, and the Total (of each 100 scores) of the leaderboards for values.
UPDATED:
So I have updated my answer using CrimsonChris' help. The only question I have is, how can I know when it is done totaling the score for all 32 teams? The reason I ask, is because I would then like to organize them from highest to lowest, and display them in that order in a tableView.
This is what I've updated my answer to:
In my viewDidLoad:
- (void)loadLeaderboardData {
// Array of leaderboard ID's to get high scores for
NSArray *leaderboardIDs = #[#"algeria", #"argentina", #"australia", #"belgium", #"bosniaandherzegovina", #"brazil", #"cameroon", #"chile", #"colombia", #"costarica", #"croatia", #"ecuador", #"england", #"france", #"germany", #"ghana", #"greece", #"honduras", #"iran", #"italy", #"ivorycoast", #"japan", #"mexico", #"netherlands", #"nigeria", #"portugal", #"russia", #"southkorea", #"spain", #"switzerland", #"unitedstates", #"uruguay"];
scoresByLeaderboardID = [NSMutableDictionary dictionary];
__block int requestCount = (int)leaderboardIDs.count;
for (NSString *leaderboardID in leaderboardIDs) {
[self loadScoresForLeaderboardID:leaderboardID range:NSMakeRange(1, 100) callback:^(NSArray *scores) {
scoresByLeaderboardID[leaderboardID] = scores;
if (--requestCount <= 0) {
if (callback)callback(scoresByLeaderboardID);
}
}];
}
}
- (void)loadScoresForLeaderboardID:(NSString*)leaderboardID range:(NSRange)range callback:(CallbackBlock)callback {
__block int totalScore = 0;
GKLeaderboard *myLB = [[GKLeaderboard alloc] init];
myLB.identifier = leaderboardID;
myLB.timeScope = GKLeaderboardTimeScopeAllTime;
myLB.playerScope = GKLeaderboardPlayerScopeGlobal;
myLB.range = range;
[myLB loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
if (error != nil) {
//NSLog(#"%#", [error localizedDescription]);
}
if (scores != nil) {
for (GKScore *score in scores) {
//NSLog(#"Individual Scores: %lld (For %#)", score.value, leaderboardID);
}
}
}];
}
Your method can be cleaned up by using callback blocks.
typedef void(^CallbackBlock)(id object);
//callback accepts an NSArray* of GKScore*
- (void)loadScoresForLeaderboardID:(NSString *)leaderboardID range:(NSRange)range callback:(CallbackBlock)callback {
GKLeaderboard *myLB = [[GKLeaderboard alloc] init];
myLB.identifier = leaderboardID;
myLB.timeScope = GKLeaderboardTimeScopeAllTime;
myLB.playerScope = GKLeaderboardPlayerScopeGlobal;
myLB.range = range;
[myLB loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
if (error != nil) NSLog(#"%#", [error localizedDescription]);
if (callback) callback(scores);
}];
}
You can then loop over your leaderboards.
//callback accepts an NSDictionary* of NSArray*(of GKScore*) by NSNumber*
- (void)loadScoresForLeaderboardIDs:(NSArray *)leaderboardIDs withCallback:(CallbackBlock)callback {
NSMutableDictionary *scoresByLeaderboardID = [NSMutableDictionary dictionary];
__block int requestCount = leaderboardIDs.count;
for (NSString *leaderboardID in leaderboardIDs) {
[LeaderboardLoader loadScoresForLeaderboardID:leaderboardID range:NSMakeRange(1, 100) callback:^(NSArray *scores) {
scoresByLeaderboardID[leaderboardID] = scores;
if (--requestCount <= 0) { //not thread safe
if (callback) callback(scoresByLeaderboardID);
}
}];
}
}
Once this method fires its callback you should have all your scores.
[LeaderboardLoader loadScoresForLeaderboardIDs:#[#"FirstID", #"SecondID", #"ThirdID"] withCallback:^(NSDictionary *scoresByLeaderboardID) {
NSLog(#"%#", scoresByLeaderboardID);
//do whatever you need to with all your scores here.
}];