Skip to main content
Commonmark migration
Source Link

#StackKit (Objective-C)

StackKit (Objective-C)

#StackKit (Objective-C)

StackKit (Objective-C)

added code comments
Source Link
Dave DeLong
  • 5.3k
  • 1
  • 17
  • 12
SKSite * site = [SKSite stackAppsSite];

//begin building our request for all questions  
SKFetchRequest * request = [[SKFetchRequest alloc] init];
[request setEntity:[SKQuestion class]]; 

//this will hold how the favorited counts for a user
NSCountedSet * counts = [NSCountedSet set];

NSUInteger count = NSUIntegerMax;
//retrieve all the questions
for (NSUInteger offset = 0; offset < count; offset += 100) {
    //starting from the appropriate offset (page)
    [request setFetchOffset:offset];
    
    NSError * error = nil;
    NSArray * matches = [site executeSynchronousFetchRequest:request error:&error];

    //loop through all the questions and record how many times they've been favorited
    for (SKQuestion * question in matches) {
        NSUInteger count = [[question favoriteCount] unsignedIntegerValue];
        for (int i = 0; i < count; ++i) { [counts addObject:[question ownerID]]; }
    }
    
    if (count == NSUIntegerMax) {
        count = [[request fetchTotal] unsignedIntegerValue];
    }
}
[request release]; 

//turn the counted set into a dictionary (so we can sort it)
NSMutableArray * favoriteCounts = [NSMutableArray array];
for (id user in counts) {
    [favoriteCounts addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                               user, @"user",
                               [NSNumber numberWithUnsignedInteger:[counts countForObject:user]], @"count",
                               nil]];
}
//sort the dictionary by the count, descending
[favoriteCounts sortUsingDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"count" ascending:NO] autorelease]]];

//create a request to fetch all the user objects
request = [[SKFetchRequest alloc] init];
[request setEntity:[SKUser class]];
[request setPredicate:[NSPredicate predicateWithFormat:@"%K = %@", SKUserID, [favoriteCounts valueForKey:@"user"]]];

NSArray * users = [site executeSynchronousFetchRequest:request error:nil];
[request release];

//build a userID => user map
NSDictionary * userMapping = [NSDictionary dictionaryWithObjects:users forKeys:[users valueForKey:SKUserID]]; 

//print out the favorited count and username
for (NSDictionary * top in favoriteCounts) {
    SKUser * user = [userMapping objectForKey:[top objectForKey:@"user"]];
    NSLog(@"%2d - %@ [%@]", [[top objectForKey:@"count"] unsignedIntegerValue], [user displayName], [user userID]);
}
SKSite * site = [SKSite stackAppsSite];

SKFetchRequest * request = [[SKFetchRequest alloc] init];
[request setEntity:[SKQuestion class]];

NSCountedSet * counts = [NSCountedSet set];

NSUInteger count = NSUIntegerMax;
for (NSUInteger offset = 0; offset < count; offset += 100) {
    [request setFetchOffset:offset];
    
    NSError * error = nil;
    NSArray * matches = [site executeSynchronousFetchRequest:request error:&error];
    for (SKQuestion * question in matches) {
        NSUInteger count = [[question favoriteCount] unsignedIntegerValue];
        for (int i = 0; i < count; ++i) { [counts addObject:[question ownerID]]; }
    }
    
    if (count == NSUIntegerMax) {
        count = [[request fetchTotal] unsignedIntegerValue];
    }
}
[request release];

NSMutableArray * favoriteCounts = [NSMutableArray array];
for (id user in counts) {
    [favoriteCounts addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                               user, @"user",
                               [NSNumber numberWithUnsignedInteger:[counts countForObject:user]], @"count",
                               nil]];
}
[favoriteCounts sortUsingDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"count" ascending:NO] autorelease]]];

request = [[SKFetchRequest alloc] init];
[request setEntity:[SKUser class]];
[request setPredicate:[NSPredicate predicateWithFormat:@"%K = %@", SKUserID, [favoriteCounts valueForKey:@"user"]]];

NSArray * users = [site executeSynchronousFetchRequest:request error:nil];
[request release];

NSDictionary * userMapping = [NSDictionary dictionaryWithObjects:users forKeys:[users valueForKey:SKUserID]];

for (NSDictionary * top in favoriteCounts) {
    SKUser * user = [userMapping objectForKey:[top objectForKey:@"user"]];
    NSLog(@"%2d - %@ [%@]", [[top objectForKey:@"count"] unsignedIntegerValue], [user displayName], [user userID]);
}
SKSite * site = [SKSite stackAppsSite];

//begin building our request for all questions  
SKFetchRequest * request = [[SKFetchRequest alloc] init];
[request setEntity:[SKQuestion class]]; 

//this will hold how the favorited counts for a user
NSCountedSet * counts = [NSCountedSet set];

NSUInteger count = NSUIntegerMax;
//retrieve all the questions
for (NSUInteger offset = 0; offset < count; offset += 100) {
    //starting from the appropriate offset (page)
    [request setFetchOffset:offset];
    
    NSError * error = nil;
    NSArray * matches = [site executeSynchronousFetchRequest:request error:&error];

    //loop through all the questions and record how many times they've been favorited
    for (SKQuestion * question in matches) {
        NSUInteger count = [[question favoriteCount] unsignedIntegerValue];
        for (int i = 0; i < count; ++i) { [counts addObject:[question ownerID]]; }
    }
    
    if (count == NSUIntegerMax) {
        count = [[request fetchTotal] unsignedIntegerValue];
    }
}
[request release]; 

//turn the counted set into a dictionary (so we can sort it)
NSMutableArray * favoriteCounts = [NSMutableArray array];
for (id user in counts) {
    [favoriteCounts addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                               user, @"user",
                               [NSNumber numberWithUnsignedInteger:[counts countForObject:user]], @"count",
                               nil]];
}
//sort the dictionary by the count, descending
[favoriteCounts sortUsingDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"count" ascending:NO] autorelease]]];

//create a request to fetch all the user objects
request = [[SKFetchRequest alloc] init];
[request setEntity:[SKUser class]];
[request setPredicate:[NSPredicate predicateWithFormat:@"%K = %@", SKUserID, [favoriteCounts valueForKey:@"user"]]];

NSArray * users = [site executeSynchronousFetchRequest:request error:nil];
[request release];

//build a userID => user map
NSDictionary * userMapping = [NSDictionary dictionaryWithObjects:users forKeys:[users valueForKey:SKUserID]]; 

//print out the favorited count and username
for (NSDictionary * top in favoriteCounts) {
    SKUser * user = [userMapping objectForKey:[top objectForKey:@"user"]];
    NSLog(@"%2d - %@ [%@]", [[top objectForKey:@"count"] unsignedIntegerValue], [user displayName], [user userID]);
}
added 969 characters in body
Source Link
Dave DeLong
  • 5.3k
  • 1
  • 17
  • 12

Calls made:

http://api.stackapps.com/1.0/questions?body=true&key=<key>&pagesize=100&page=1
http://api.stackapps.com/1.0/questions?body=true&key=<key>&pagesize=100&page=2
http://api.stackapps.com/1.0/questions?body=true&key=<key>&pagesize=100&page=3
http://api.stackapps.com/1.0/questions?body=true&key=<key>&pagesize=100&page=4
http://api.stackapps.com/1.0/questions?body=true&key=<key>&pagesize=100&page=5
http://api.stackapps.com/1.0/questions?body=true&key=<key>&pagesize=100&page=6
http://api.stackapps.com/1.0/users/2;18;14;825;11;5;51;40;956;816;448;29;9;13;30;4;1241;33;839;192;1026;614;1290;42;19;8;700;1031;570;377;1265;17;222;199;938;449;1048;2292;451;39;1395;28;2192;1049;1394;113;693;358;1849;186;638;70;664;91;140;1221;1263;171;127;295;2296;2734;151;2287;44;757;375;424;431;2418;1689;2467;2679;1179;1978;1270;138;2083;525;2442;234;1578;43;2829;20;239;2883;216;2203;505;1856;2662;23;680?key=<key>&page=1&pagesize=100

Calls made:

http://api.stackapps.com/1.0/questions?body=true&key=<key>&pagesize=100&page=1
http://api.stackapps.com/1.0/questions?body=true&key=<key>&pagesize=100&page=2
http://api.stackapps.com/1.0/questions?body=true&key=<key>&pagesize=100&page=3
http://api.stackapps.com/1.0/questions?body=true&key=<key>&pagesize=100&page=4
http://api.stackapps.com/1.0/questions?body=true&key=<key>&pagesize=100&page=5
http://api.stackapps.com/1.0/questions?body=true&key=<key>&pagesize=100&page=6
http://api.stackapps.com/1.0/users/2;18;14;825;11;5;51;40;956;816;448;29;9;13;30;4;1241;33;839;192;1026;614;1290;42;19;8;700;1031;570;377;1265;17;222;199;938;449;1048;2292;451;39;1395;28;2192;1049;1394;113;693;358;1849;186;638;70;664;91;140;1221;1263;171;127;295;2296;2734;151;2287;44;757;375;424;431;2418;1689;2467;2679;1179;1978;1270;138;2083;525;2442;234;1578;43;2829;20;239;2883;216;2203;505;1856;2662;23;680?key=<key>&page=1&pagesize=100
Post Made Community Wiki
Source Link
Dave DeLong
  • 5.3k
  • 1
  • 17
  • 12
Loading