SQL - Disconnect All Users

by Victor 6. April 2012 03:21

Since I always seem to forget this, this is an SQL snippet you can run to disconnect all users from a database. Just replace the database name.

 

USE master;
GO
ALTER DATABASE AdventureWorks2012
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO

ALTER DATABASE AdventureWorks2012
SET MULTI_USER;
GO

Tags:

SQL

Component/Entity System Frameworks

by Victor 16. March 2012 17:35

I've been scrounging the internet for a C++ implementation of en entity system or component framework and have come across many. Since there is no single source of frameworks, I thought I'd list what I've found here.

 

Name

Language

URL

Blog Post

Artemis Java http://gamadu.com/artemis  
Artemis C# http://ploobs.com.br/?p=1765  
Artemis C++   http://www.gamedev.net/blog/1390/entry-2254064-c-port-of-artemis-entity-component-system-framework-in-progress/
Ash ActionScript https://github.com/richardlord/Ash http://www.richardlord.net/blog/what-is-an-entity-framework
Cistron C++ http://code.google.com/p/cistron/  
Trefall C++ http://svn2.xp-dev.com/svn/Trefall-ComponentSystem/Source http://www.gamedev.net/topic/551515-component-based-entities-using-properties

 

More to come as I collect them all. If you know of any you want me to add, please leave a comment.

 

Entity System/Component-based articles/posts:

http://t-machine.org/index.php/2010/05/09/entity-system-1-javaandroid/

http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/

http://www.purplepwny.com/blog/?p=215

http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/

http://entity-systems.wikidot.com/es-approaches

Tags: , , , , ,

Game Programming | Software Development

iOS GCD to perform asynchronous tasks

by Victor 19. February 2012 01:20

GCD stands for Grand Central Dispatch. It's an API available in iOS4 and above to facilitate concurrent programming. Using the API, you can execute long executing tasks in the background.

 

For example, say you want to delete a temporary folder but want to make sure the main thread isn't blocked while you're doing it. The code below will do just that:

 

// Create a dispatch queue
dispatch_queue_t log_queue = dispatch_queue_create("log_queue", NULL);
// Add dispatch queue to GCD
dispatch_async(log_queue, ^{
        NSFileManager *manager = [NSFileManager defaultManager];
        NSString *logsDir = [NSString stringWithFormat:@"%@/Logs", [ACUtility applicationDocumentDirectory]];
        [manager removeItemAtPath:logsDir error:nil];
});
// Release the queue for memory management
dispatch_release(log_queue);

 

 

Tags: , , , , , ,

iOS | Software Development

UITextField in iOS5 shows white corners running on iOS4

by Victor 27. January 2012 18:15

While working on a new iPhone project, I came across an issue with UITextField. I created a new project in Xcode 4.2 using the iOS5 SDK but targeting iOS4 to support older devices. I added a UITextField to a view and ran the program. In iOS5 the text field looks fine but in iOS 4.x it has white corners like so:

 

 

This is obviously not desired. To fix the issue, I had to do the following:

  1. Add the QuartzCore framework to the project and insert the import statement
  2. Set the border style to None
  3. Set the UITextField's layer corner radius to some value (I chose 8.0f)
  4. Set the UITextField's layer masksToBounds property to YES
 
self.tbxFloor.borderStyle = UITextBorderStyleNone;
self.tbxFloor.layer.cornerRadius = 8.0f;
self.tbxFloor.layer.masksToBounds = YES;
 
 
and now it looks like this
 
 

Tags: , , , , , ,

iTunes and App Store links

by Victor 14. January 2012 01:14

I was searching online for a way to direct a user to the review page in the app store and also a way to direct a user to the app update page if the current version is not the latest. Well, I found that and MORE.

This is the link you would use to send the user to the review page:

itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=[APPID]

where the value for id should be your App ID from iTunes Connect.

This is the url you would use to direct a user to the app store update page:

tms-apps://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftwareUpdate?id=[APPID]&mt=8

I also found this post that has many other useful urls:

http://bjango.com/articles/ituneslinks/

Tags: , , , , ,

iOS | Mobile | Software Development

NSManagedObject scalar property

by Victor 13. January 2012 02:24

If you add a scalar property, such as a bool, int, or float, to your object model, Core Data will create a property of the type NSNumber, like so:

 

@property (nonatomic, retain) NSNumber* duration;

You could simply write all of your code that uses that property to convert from scalar to NSNumber back and forth, like so:

 

float myDuration = [myOjbect.duration floatValue];
myObject.duration = [NSNumber* numberWithFloat:myDuration];

 

and that'll work just fine. But, if you prefer using a property that is the actual type you want to work with in the code you could declare the property as a scalar instead, like so:

 

@property float duration;

 

and your implementation file would look like this:

 

@interface MyManagedObject (PrimitiveAccessors)
@property (nonatomic, retain) NSNumber primitiveDuration;
@end
 
 
- (double)duration
{
    [self willAccessValueForKey:@"duration"];
    NSNumber *tmpValue = [self primitiveDuration];
    [self didAccessValueForKey:@"duration"];
    return (tmpValue!=nil) ? [tmpValue floatValue] : 0.0;
}
 
- (void)setDuration:(double)value
{
    NSNumber* temp = [[NSNumber alloc] initWithFloat: value];
    [self willChangeValueForKey:@"duration"];
    [self setPrimitiveDuration:temp];
    [self didChangeValueForKey:@"duration"];
    [temp release];
}

 

For more information, see the Apple docs section on scalar properties in Core Data;

Tags: , ,

iOS | Mobile | Software Development

HTTP status codes

by Victor 12. January 2012 19:18

Borrowed from this site:

Registry:
Value     Description                              Reference
--------  ---------------------------------------  ---------
100       Continue                                 [RFC2616]
101       Switching Protocols                      [RFC2616]
102       Processing                               [RFC2518]
103-199   Unassigned
200       OK                                       [RFC2616]
201       Created                                  [RFC2616]
202       Accepted                                 [RFC2616]
203       Non-Authoritative Information            [RFC2616]
204       No Content                               [RFC2616]
205       Reset Content                            [RFC2616]
206       Partial Content                          [RFC2616]
207       Multi-Status                             [RFC4918]
208       Already Reported                         [RFC5842]
209-225   Unassigned
226       IM Used                                  [RFC3229]
227-299   Unassigned
300       Multiple Choices                         [RFC2616]
301       Moved Permanently                        [RFC2616]
302       Found                                    [RFC2616]
303       See Other                                [RFC2616]
304       Not Modified                             [RFC2616]
305       Use Proxy                                [RFC2616]
306       Reserved                                 [RFC2616]
307       Temporary Redirect                       [RFC2616]
308-399   Unassigned
400       Bad Request                              [RFC2616]
401       Unauthorized                             [RFC2616]
402       Payment Required                         [RFC2616]
403       Forbidden                                [RFC2616]
404       Not Found                                [RFC2616]
405       Method Not Allowed                       [RFC2616]
406       Not Acceptable                           [RFC2616]
407       Proxy Authentication Required            [RFC2616]
408       Request Timeout                          [RFC2616]
409       Conflict                                 [RFC2616]
410       Gone                                     [RFC2616]
411       Length Required                          [RFC2616]
412       Precondition Failed                      [RFC2616]
413       Request Entity Too Large                 [RFC2616]
414       Request-URI Too Long                     [RFC2616]
415       Unsupported Media Type                   [RFC2616]
416       Requested Range Not Satisfiable          [RFC2616]
417       Expectation Failed                       [RFC2616]
422       Unprocessable Entity                     [RFC4918]
423       Locked                                   [RFC4918]
424       Failed Dependency                        [RFC4918]
425       Reserved for WebDAV advanced             [RFC2817] 
          collections expired proposal
426       Upgrade Required                         [RFC2817]
427-499   Unassigned
500       Internal Server Error                    [RFC2616]
501       Not Implemented                          [RFC2616]
502       Bad Gateway                              [RFC2616]
503       Service Unavailable                      [RFC2616]
504       Gateway Timeout                          [RFC2616]
505       HTTP Version Not Supported               [RFC2616]
506       Variant Also Negotiates (Experimental)   [RFC2295]
507       Insufficient Storage                     [RFC4918]
508       Loop Detected                            [RFC5842]
509       Unassigned
510       Not Extended                             [RFC2774]
511-599   Unassigned

 

NSDateFormatter formatting strings reference

by Victor 12. January 2012 19:14

Borrowed from this post.

 

a:	AM/PM
A:	0~86399999 (Millisecond of Day)
 
c/cc:	1~7 (Day of Week)
ccc:	Sun/Mon/Tue/Wed/Thu/Fri/Sat
cccc:	Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday
 
d:	1~31 (0 padded Day of Month)
D:	1~366 (0 padded Day of Year)
 
e:	1~7 (0 padded Day of Week)
E~EEE:	Sun/Mon/Tue/Wed/Thu/Fri/Sat
EEEE:	Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday
 
F:	1~5 (0 padded Week of Month, first day of week = Monday)
 
g:	Julian Day Number (number of days since 4713 BC January 1)
G~GGG:	BC/AD (Era Designator Abbreviated)
GGGG:	Before Christ/Anno Domini
 
h:	1~12 (0 padded Hour (12hr))
H:	0~23 (0 padded Hour (24hr))
 
k:	1~24 (0 padded Hour (24hr)
K:	0~11 (0 padded Hour (12hr))
 
L/LL:	1~12 (0 padded Month)
LLL:	Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
LLLL:	January/February/March/April/May/June/July/August/September/October/November/December
 
m:	0~59 (0 padded Minute)
M/MM:	1~12 (0 padded Month)
MMM:	Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
MMMM:	January/February/March/April/May/June/July/August/September/October/November/December
 
q/qq:	1~4 (0 padded Quarter)
qqq:	Q1/Q2/Q3/Q4
qqqq:	1st quarter/2nd quarter/3rd quarter/4th quarter
Q/QQ:	1~4 (0 padded Quarter)
QQQ:	Q1/Q2/Q3/Q4
QQQQ:	1st quarter/2nd quarter/3rd quarter/4th quarter
 
s:	0~59 (0 padded Second)
S:	(rounded Sub-Second)
 
u:	(0 padded Year)
 
v~vvv:	(General GMT Timezone Abbreviation)
vvvv:	(General GMT Timezone Name)
 
w:	1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year)
W:	1~5 (0 padded Week of Month, 1st day of week = Sunday)
 
y/yyyy:	(Full Year)
yy/yyy:	(2 Digits Year)
Y/YYYY:	(Full Year, starting from the Sunday of the 1st week of year)
YY/YYY:	(2 Digits Year, starting from the Sunday of the 1st week of year)
 
z~zzz:	(Specific GMT Timezone Abbreviation)
zzzz:	(Specific GMT Timezone Name)
Z:	+0000 (RFC 822 Timezone)

Tags: , , , ,

iOS

Visual Studio 2010 and .NET 4.0 - Useful links

by Victor 31. December 2011 01:04

.Net Asynchronous tasks

by Rod 28. December 2011 20:21

Recently I was in need of increasing the performance of a lengthy task - Generating a PDF out of a set of data; which consists of "screenshots" of a series of webpages, set in certain order.

The initial design of the "generating" tool consisted of giving the generating method a list of URLs, then query the webpages in the given order (each URL is a page in the document), and then compile the document. The library we utilized to generate the pdf document requires that each page's generation be done in an STA environment, therefore, each page was generated on its own individual thread; and these were done sequentially; with a wait at the end of each page.

To increase the performance, we decided to make use of the existing threading and use a simple logic to enable multithreading so that all pages are generated simultaneously.

List<Thread> generatingThreads = new List<Thread>();

foreach (string parameter in listOfParameters)
{

     ThreadParameter threadParameters = new ThreadParameter { parameter };
     ParameterizedThreadStart pts = new ParameterizedThreadStart(PDFGenerator.ConvertPage);
     Thread t = new Thread(pts);
     generatingThreads.Add(t);
     t.SetApartmentState(ApartmentState.STA);
     t.Start(parameters);
}

bool conversionCompleted = false;

while (!conversionCompleted) //Wait until all pages are converted
{
      conversionCompleted = true;
      foreach (var thread in generatingThreads)
      {
           if (!thread.IsAlive)
           {
                 continue;
            }
            else
            {
                 conversionCompleted = false;
            }
     }
}

Notice how each conversion's thread is added to a list, and after all threads have been started; a loop prevents the code from continuing and completing the document creation until all conversions are finished. This logic is crude and probably there are better methods of accomplishing the same result; but for our simple scenario, it works remarkably well.

However, we still had a big problem - the process still took over 20 seconds, and the user would be wondering whether the process has not gotten stuck! Therefore, we decided to improve the process even further by making the process asynchronous. That is: Perform the generation task on a different thread and return control to the web application immediately. The advantage is obvious - the user is never let wondering whether the application has not gone "bad" and can continue working while the pdf (or any other lengthy task) goes on. The only disadvantage is that the results of the lengthy task (the pdf in our scenario) is not immediately available. In our case, this seemed a reasonable tradeoff.

The .Net framework makes the task of making ANY method asynchronous very easy. All you need to do is create a delegate method declaration and call  the delegate method asynchronously. Let me walk you through the steps:

Say the method you wish to convert is

public void ITakeALongTime(List<string> parameters1, int parameter2) {...}

which you normally call like this: 

myClass.ITakeALongTime(new List<string> { "param1", "param2", "param3"}, 42);

Then, what you need to do is declare the delegate like so:

public delegate void DelegateITakeALongTime(List<string> parameters1, int parameter2);

and call the method asynchronously like so:

public void ITakeALongTimeAsynchronous(List<string> parameters1, int parameter2)
{
       // create the delegate
       DelegateITakeALongTime theDelegate = new DelegateITakeALongTime(ITakeALongTime);

       // call the BeginInvoke
       IAsyncResult tag = theDelegate.BeginInvoke(parameters1, parameter2, null, null);

 }

And that's it! When BeginInvoke is called, ITakeALongTime, thru the delegate, will be called asynchronously. Something worth noticing is that BeginInvoke, as we are using it, takes a list of parameters where the first parameters are the parameters that will be passed to the delegate (and then to the lengthy process that will be executed asynchronously), and the last parameters "null, null" are not part of the of our method but actually part of the begin invoke; which for our simple scenario, are null and null.

Note: IAsyncResult resides in System.Windows.Forms

There are more things that can be done; like subscribing to the end event (for instance, to notify the user the task has finished), but in our scenario there are not necessary; hence I've not delved on them. See this MSDN post for more information, patterns, and examples.

 

Hoping this helps you,

Stay coding my friends!

Rod

Tags:

ASP.NET

Powered by BlogEngine.NET 2.5.0.6

About Us

Wuji Touch is a software consulting firm based in Delray Beach, Florida. We specialize in Microsoft technologies, but we also have experience with Progress OpenEdge, iOS, and Android development. This is our blog. These are the tales of the pains and pleasures we experience daily in our journey through 1s and 0s.

Month List

RecentPosts